python 3 error RuntimeError: super(): no arguments - python

Why do I get this error? Can someone solve this problem for me? I tried to call the display function from class project in Progress.display() or anybody has other solution on how to display the users input?
And how can I input Stages class and Progress class at the same time ? thanks for the helpp
super().display()
RuntimeError: super(): no arguments
Here's the code
class Project:
def __init__(self, name="", job="", **kwargs):
super().__init__(**kwargs)
self.name = name
self.job = job
def display():
print("name: ", (self.name))
print("job: ", (self.job))
#staticmethod
def prompt_init():
return dict(name=input("name: "), job=input("job: "))
class Stages(Project):
def __init__(self, stages="", **kwargs):
super().__init__(**kwargs)
self.stages = stages
def display(self):
super().display()
print("stages: ", (self.stages))
#staticmethod
def prompt_init():
parent_init = Project.prompt_init()
choice = None
while choice not in (1, 2, 3, 4, 5, 6):
print("Insert your stage now: ")
print("1. Planning")
print("2. Analysis")
print("3. Design")
print("4. Implementation")
print("5. Testing")
print("6. Release")
choice = input("enter your choice: ")
choice = int(choice)
if choice == 1:
stages = "Planning"
elif choice == 2:
stages = "Analysis"
elif choice == 3:
stages = "Design"
elif choice == 4:
stages = "Implementation"
elif choice == 5:
stages = "Testing"
elif choice == 6:
stages = "Release"
else:
print("no such input, please try again")
print(name)
print(stages)
class Progress(Project):
def __init__(self, progress="", **kwargs):
super().__init__(**kwargs)
self.progress = progress
def display(self):
super().display()
print("progress: ", (self.progress))
#staticmethod
def prompt_init():
parent_init = Project.prompt_init()
choice = None
while choice not in (1, 2, 3, 4):
print("1. 25%")
print("2. 50%")
print("3. 75%")
print("4. 100%")
choice = input("enter your choice[1-4]: ")
choice = int(choice)
if choice == 1:
progress = "25%"
elif choice == 2:
progress = "50%"
elif choice == 3:
progress = "75%"
elif choice == 4:
progress = "100%"
else:
print("no such input, please try again")
print(progress)
parent_init.update({"progress": progress})
return parent_init
class A(Stages, Progress):
def prompt_init():
init = Stages.prompt_init()
init.update(Progress.prompt_init())
return init
prompt_init = staticmethod(prompt_init)
class New:
type_map = {("stages", "progress"): A}
def add_project_test(self, name, job, stages):
init_args = Project.prompt_init()
self.project_list.append(Project(**init_args))
def __init__(self):
self.project_list = []
def display_project():
for project in self.project_list:
project.display()
print()
def add_progress(self):
init_args = Progress.prompt_init()
self.project_list.append(Progress(**init_args))
def add_project(self):
ProjectClass = self.type_map[A]
init_args = ProjectClass.prompt_init()
self.property_list.append(ProjectClass(**init_args))
my_list = New()
my_list.add_progress()
my_list.display_project()

Not 100% solution to the answer, but same error. Posted with love for Googlers who have the same issue as me.
Using Python 3, I got this error because I forgot to include self in the method. Simple thing, but sometimes the most simple things trip you up when you're tired.
class foo(object):
def bar(*args):
super().bar(*args)
=> RuntimeError: super(): no arguments
Remember to include your self
class foo(object):
def bar(self, *args):
super().bar(*args)

Not really an answer for this question, but I got this same error when trying to call super while in a pdb shell and ended up going down a rabbit hole trying to figure it out. You need to add the parent class you want to call super on and self to the call in order for it to run - super(<ParentClass>, self) - in pdb. Or at least just know that super won't work as expected in pdb. I didn't really need to call it there, but it blocked me from figuring out why something else wasn't working.

Every time you use super() in a method, you need to be in an instance method or a class method. Your staticmethods don't know what their superclasses are. Observe:
class Funky:
def groove(self):
print("Smooth")
#staticmethod
def fail():
print("Ouch!")
#classmethod
def wail(cls):
print("Whee!")
class Donkey(Funky):
def groove(self):
print(super())
#staticmethod
def fail():
try:
print(super())
except RuntimeError as e:
print("Oh no! There was a problem with super!")
print(e)
#classmethod
def wail(cls):
print(super())
a_donkey = Donkey()
a_donkey.groove()
a_donkey.fail()
a_donkey.wail()
Outputs:
<super: <class 'Donkey'>, <Donkey object>>
Oh no! There was a problem with super!
super(): no arguments
<super: <class 'Donkey'>, <Donkey object>>
Here's your code, debugged and with some extra functionality and tests:
class Project:
def __init__(self, name="", job="", **kwargs):
super().__init__(**kwargs)
self.name = name
self.job = job
def display(self):
print("name: ", self.name)
print("job: ", self.job)
#staticmethod
def prompt_init():
return dict(name=input("name: "), job=input("job: "))
class Progress(Project):
def __init__(self, progress="", **kwargs):
super().__init__(**kwargs)
self.progress = progress
def display(self):
super().display()
print("progress: ", self.progress)
#staticmethod
def prompt_init():
parent_init = Project.prompt_init()
progress = input("your progress: ")
parent_init.update({
"progress": progress
})
return parent_init
class New:
def __init__(self):
self.project_list = []
def display_project(self):
for project in self.project_list:
project.display()
print()
def add_project(self):
init_args = Project.prompt_init()
self.project_list.append(Project(**init_args))
def add_progress(self):
init_args = Progress.prompt_init()
self.project_list.append(Progress(**init_args))
my_list = New()
my_list.add_project()
my_list.add_progress()
my_list.display_project()

You might not have to use super() at all, just reference the super class directly. For example, I was writing a Django test like this one, but in my case the AnimalTestCase was inheriting a ParentTestCase. I wanted the fixture property in AnimalTestCase to use all the same fixtures in ParentTestCase, and add a couple more. But calling super() never worked. In the end, I realized that I could reference ParentTestCase as it was.
fixtures = ParentTestCase.fixtures + ['more']
class ParentTestCase(TestCase):
fixtures = ['bacteria', 'fungus', 'stalagtites', 'stalagmites']
def setUp(self):
# Test definitions as before.
call_setup_methods()
class AnimalTestCase(ParentTestCase):
fixtures = ParentTestCase.fixtures + ['vertebrata', 'invertebrate']
def test_fluffy_animals(self):
# A test that uses the fixtures.
call_some_test_code()

Related

decorating method using instance method

I am trying to decorate my instance car_details with another instance method decor but am not able to figure out why this is giving the error. while i call the call car_details method like below
if __name__ == '__main__':
car_type = input("Enter type of car: ")
price = int(input("Enter price of car: "))
obj = Car(car_type, price)
decor = obj.decor
details = obj.car_details
tax = {1: "Yes", 2: "No"}
to_apply = int(input("Select 1 for applying tax and 0 for No:"))
ans = tax.get(to_apply)
res = decor(details)
res(ans)
then it works properly, but while i try it with the #decorator at that time i get issue.
TypeError: decor() missing 1 required positional argument: 'func'
TYPE = {'hybrid': 'Hybrid', 'petrol': 'Petrol', 'deasel': 'Deasel'}
def tax_price(car_type):
if car_type == 'hybrid':
SGST = 300000
GST = 500000
elif car_type == 'petrol':
SGST = 200000
GST = 400000
elif car_type == 'deasel':
SGST = 100000
GST = 300000
return SGST+GST
class Car:
def __init__(self, car_type, price):
self.car_type = car_type
self.price = price
def decor(self, func):
def wrapper(apply_tax):
if apply_tax == 'Yes':
car_tax = tax_price(self.car_type)
self.price += car_tax
func()
else:
func()
return wrapper
#decor
def car_details(self):
print("Car Type:", self.car_type)
print("Car Price: ", self.price)
if __name__ == '__main__':
car_type = input("Enter type of car: ")
price = int(input("Enter price of car: "))
obj = Car(car_type, price)
tax = {1: "Yes", 2: "No"}
to_apply = int(input("Select 1 for applying tax and 0 for No:"))
ans = tax.get(to_apply)
wrp = obj.car_details()
wrp(ans)
I see a few problems here. The biggest problem is you seem to be trying to use a decorator when a decorator is really (and I mean really) not suitable for this situation. If this code is not experimental and is to be maintained by someone, I strong suggest simplifying to:
def car_details(self, apply_tax):
if apply_tax == 'Yes':
car_tax = tax_price(self.car_type)
self.price += car_tax
print("Car Type:", self.car_type)
print("Car Price: ", self.price)
But if you are just experimenting and want to make it work with decorators, I see the following problems:
When a decorator is called, the only arg passed to it is the function being decorated, not self which is not passed in at all. That means def decor(self, func): will need to change to def decor(func):.
When the decorated function, which is a method of a class, is invoked, the first arg will be self. That means def wrapper(apply_tax): will need to change to def wrapper(self, apply_tax):.
When the wrapped functions is invoked, it needs self as its only arg. That means func() will need to change to func(self) (in both places).
So, that gives:
def decor(func):
def wrapper(self, apply_tax):
if apply_tax == 'Yes':
car_tax = tax_price(self.car_type)
self.price += car_tax
func(self)
else:
func(self)
return wrapper
But, how does the decorated function get a value for apply_tax? It needs to be passed as an arg. So, you'll need ...
wrp = obj.car_details(ans)
Since this is a call on an instance, Python will automatically add self as the first arg and since car_details is decorated it will effectively be calling wrapper(self, ans).

I need to figure out how to save the user input as the new variables, not sure what to do from here

#I need to figure out how to save the user input as the new variables, not sure what to do from here.
class Person:
def __init__(self):
#I'm supposed to have the object set to these generic names at first and then after the prompt function they need to be updated.
self.name = 'anonymous'
self.b_year = 'unknown'
def prompt(self):
print('Please enter the following:')
self.name = input('Name: ')
self.b_year = input('Year: ')
b = Book()
b.prompt()
def display(self):
print(f'Author:\n{self.name} (b. {self.b_year})\n')
class Book:
def __init__(self):
self.title = 'untitled'
self.publisher = 'unpublished'
def prompt(self):
self.title = input('Title: ')
self.publisher = input('Publisher: ')
def display(self):
print(f'\n{self.title}\nPublisher:\n{self.publisher}')
p = Person()
`enter code here`p.display()
def main():
p = Person()
b = Book()
b.display()
p.prompt()
b.display() #right here I need it to display the new information
if __name__ == '__main__':
main()
Two things you need to do here. First of all you need to initialize your variables in your Person class. Second of all I don't understand what the point of creating objects in a class and then doing the same outside of the class. I suggest taking the following code and modifying it to your likings. Additionally I reorganized the way you call your methods. First you prompt the user with all the necessary questions, then display them all together.
class Person:
def __init__(self):
self.name = None
self.b_year = None
def prompt(self):
print('Please enter the following:')
self.name = input('Name: ')
self.b_year = input('Year: ')
def display(self):
print(f'Author:\n{self.name} (b. {self.b_year})\n')
class Book:
def __init__(self):
self.title = 'untitled'
self.publisher = 'unpublished'
def prompt(self):
self.title = input('Title: ')
self.publisher = input('Publisher: ')
def display(self):
print(f'\n{self.title}\nPublisher:\n{self.publisher}')
def main():
p = Person()
b = Book()
b.display()
p.display()
p.prompt()
b.prompt()
b.display()
p.display()#right here I need it to display the new information
if __name__ == '__main__':
main()
output
untitled
Publisher:
unpublished
Author:
None (b. None)
Please enter the following:
Name: a
Year: b
Title: c
Publisher: d
c
Publisher:
d
Author:
a (b. b)
I'm not designer so you don't have to go with my idea, but IMO, don't add \n like this '\n{self.title}\nPublisher:\n{self.publisher}'. It looks a bit weird in the output.
You create one Book in the main function, and then another Book inside the Person.prompt method. They aren't the same Book - you have two!
You need to decide if a book belongs within a person, or whether they are two top level objects that may have a relationship. From a "real world" perspective, I don't think a person should automatically have a book associated with them. Perhaps you could create a Book and then pass to the Person constructor...

How to assign a class with an if statement in Python

I have two different devices I use in the lab. I would like to use input from command line to change which device I am using. To elaborate, I would like to type in the name of the device and a variable is defined by that name inside a if statement. Currently when I try the below code I get:
AttributeError: class temp_class has no attribute 'get_int'
What am I doing wrong
from classA import*
from classB import*
from temp_class import*
tempC = temp_class()
print tempC
user_input = raw_input()
print user_input
if (user_input == "a") :
tempC.__del__()
tempC = class_a(5)
if (user_input == 'b') :
tempC = class_b(5)
print temp_class
tempC.set_int(5)
print temp_class.get_int()
Output of code:
What is inside temp_class
class temp_class:
def __init__(self):
self.num = 8
What is inside classA
class class_a(object):
def __init__(self, int):
self.num = self.set_int(int)
def set_int(self, int):
self.num = int
def get_int(self):
return self.num
what is inside class_b
class class_b:
def __init__(self):
self.num = 8
There is a lot of trouble with the code you've shared. I'll try to simplify it:
# prog.py
from device_a import DeviceA
from device_b import DeviceB
user_input = raw_input() # use input() if you have python3
if (user_input == "a") :
# no need to create an initial "temp" instance or to delete it
tempC = DeviceA(5)
elif (user_input == 'b') :
tempC = DeviceB() # DeviceB doesn't take any parameters
# tempC can either be a DeviceA or DeviceB, but both need to have the same methods defined
try:
tempC.set_int(5) # this will fail for DeviceB because it has no set_int method
except: # this should have a more specific exception type
pass
print tempC.get_int() # note that this is the name of the variable, not the class
# device.py
class Device(object):
def __init__(self, num):
self.num = num
def get_int(self):
return self.num
# device_a.py
from device import Device
class DeviceA(Device):
def __init__(self, num): # int is a builtin function, so it shouldn't be used as a parameter name
super(DeviceA, self).__init__(num)
def set_int(self, num):
self.num = num
# device_b.py
from device import Device
class DeviceB(Device):
def __init__(self):
super(DeviceB, self).__init__(8)
# no set_int, as this one seems to be fixed in your example
Last night I figure out how to fix my problem. Below is what I am now using.
obj_dic = {}
print obj_dic
print("a for class a for class b")
user_input = raw_input(":> ")
if user_input == 'a':
obj_dic = {'a':class_a()}
else:
obj_dic = {'b':class_b()}
tempC = obj_dic.get(user_input)
print tempC.get_int()
print tempC

How to Randomly pick from list in the class

I want to randomly pick a weapon and i want to write the name of it but the result is not like i expect what is wrong in that code?
import random
class Dusman:
def __init__(self,name='',weapon='',armor=''):
self.name= name
self.weapon= weapon
self.armor= armor
def name(self):
a=name
a = input("Write a name: ")
def weapon(self):
weapon=["Sword","Axe","Topuz"]
print(random.choice(weapon))
def print(self):
print("Name",self.name,"Weapon: ",self.weapon,"Armor: ",self.armor)
dusman1=Dusman()
dusman1.name
dusman1.weapon
dusman1.print()
Is this your expected result?
import random
class Dusman:
def __init__(self,name='',weapon='',armor=''):
self._name= name
self._weapon= weapon
self._armor= armor
def name(self):
self._name = input("Write a name: ")
def weapon(self):
weapons=["Sword","Axe","Topuz"]
self._weapon = random.choice(weapons)
print(self._weapon)
def __str__(self):
return "Name: {0} Weapon: {1} Armor: {2}".format(self._name,
self._weapon,
self._armor)
if __name__ == '__main__':
dusman1=Dusman()
dusman1.name()
dusman1.weapon()
print(dusman1)
Your problem is, that you have naming collisions. You are naming a variable the same as a function.
Currently, you only print the choice.
You need to set the result of the choice to the weapon instance variable:
def weapon(self):
weapons = ["Sword", "Axe", "Topuz"]
self.weapon = random.choice(weapons)

Homework Help - Object-Oriented Programming

In my intro class, we just started the section on object-oriented programming. This class is the first I've ever been exposed to programming, and I'm really not understanding it.
We have an assignment where we have to create an Animal class, a Zoo class, and then a zookeeper program to run the information from the first two classes. I have the programs typed up based off of examples in my book, but am still not doing it correctly.
If you could look over my codes and give me some feedback or help, that would be greatly appreciated!
class Animal:
def __innit__(self, animal_type, name):
self.animal_type = animal_type
self.name = name
def get_animal_type(self, animal_type):
self.__animal_type = animal_type
def get_name(self, name):
self.__name = name
def check_mood(self, mood):
input random.txt
print random.random()
Class Zoo:
def __innit__(self):
self.__animals = animal_list
def add_animals(self, animal):
self.__animals.append(animal)
def show_animals(animal_list):
return animal_list
input Animal.py
input Zoo.py
def main():
ADD_ANIMAL = 1
SHOW_ANIMALS = 2
EXIT = 3
def get_manu_choice():
print()
print("Zoo Options")
print("-----------")
print("1. Add Animal")
print("2. Show Animals")
print("3. Exit")
print()
choice = int(input("What would you like to do? "))
while choice < ADD_ANIMAL or choice > EXIT:
choice = int(input("Please choose a valid option: "))
return choice
main()
innit should be init
Looks to me like you are missing a chunk of functionality. You need an instance of zoo, and then in response to the input, either add another animal to the zoo or print the list of animals.

Categories