Why it says that super() takes atleast 1 argument - python

class Member():
def __init__(self, MemberName, MemberID, SubcribtionPaid):
self.__MemberName = MemberName
self.__MemberID = MemberID
self.__SubcribtionPaid = False
def SetMemberName(self):
print(MemberName,self.__MemberName)
def SetMemberID (self):
print(MemberID,self.__MemberID)
def SetSubcriptionPaid(self):
print(SubcribtionPaid,self.__SubcribtionPaid)
class Jmember(Member):
def__init__(self,MemberName,MemberID,SubcribtionPaid,DateofJoining):
super().__init__(MemberName,MemberID,SubcribtionPaid)
self.__DateofJoining = DateofJoining
def SetDateofBirth(self):
print(DateofBirth,self.__DateofJoining)
NewMember = Jmember("Armeen","1245","True","12/3/2015")
NewMember.SetMemberName()
NewMember.SetMemberID()
NewMember.SetSubcriptionPaid()
NewMember.SetDateofJoining()
I basically copy pasted the code.I used pycharm. I still dont get how to use code block. I dont know what to do.
Sorry if i caused a problem.
I wrote a code in python,where one class inheritates the attributes of another class. When i run the code , error occurs on line 26 saying that super() takes atleast 1 argument .Even though i wrote the arguments. Did I make a mistake there. The code, the traceback
I want to see the output of the new member that includes member name, id , subcribtion paid and date of joining. But error is shown.

Updated my post to reflect your posted code:
You said that you are using PyCharm. pay attention to the red squiggly lines under sections as they are pointing out errors.
First error: you did not have a space between def and init
def__init__(self, MemberName, MemberID, SubcribtionPaid):
Second Error: found in your class function calls:
You do not have to call the class that is essentially what (self) is doing
def SetMemberName(self):
print(MemberName,self.__MemberName)
Third error: was found in your Traceback
You were trying to call a function that you forgot to create
Traceback (most recent call last):
File "/Users/sumbody/Projects/Python/MemberTest2/main.py", line 29, in <module>
NewMember.SetDateofJoining()
AttributeError: 'Jmember' object has no attribute 'SetDateofJoining'
Here is the working code:
class Member():
def __init__(self, MemberName, MemberID, SubcribtionPaid):
self.__MemberName = MemberName
self.__MemberID = MemberID
self.__SubcribtionPaid = False
def SetMemberName(self):
print(self.__MemberName)
def SetMemberID (self):
print(self.__MemberID)
def SetSubcriptionPaid(self):
print(self.__SubcribtionPaid)
class Jmember(Member):
def __init__(self,MemberName,MemberID,SubcribtionPaid,DateofJoining):
super().__init__(MemberName,MemberID,SubcribtionPaid)
self.__DateofJoining = DateofJoining
def SetDateofBirth(self):
print(self.__DateofJoining)
def SetDateofJoining(self):
print(self.__DateofJoining)
NewMember = Jmember("Armeen","1245","True","12/3/2015")
NewMember.SetMemberName()
NewMember.SetMemberID()
NewMember.SetSubcriptionPaid()
NewMember.SetDateofJoining()
Some advice, when learning to code and debug your own code, it is not a bad thing to take some code that runs and then break it. It sounds weird, but in doing so you will learn to recognize errors when seen in the editor and at runtime. Happy coding!

Related

Inner classes definitions keeps asking for self argument

Hello everyone hope you are well.
I am having some trouble trying to using the self variable for the innerclass however the outerclass is working just fine
#Assume this file is named classes.py
class outerclass():
def __init__(self):
self.ina = 10
self.inb = 20
self.inc = 30
class innerclass(object):
def defined(self):
self.vara = 40
self.varb = 50
I call the class like this
import classes
test = classes.outerclass()
test.innerclass.defined()
Output:
Traceback (most recent call last):
File "<pyshell#501>", line 1, in <module>
test.innerclass.defined()
TypeError: defined() missing 1 required positional argument: 'self'
Can anyone help me I have been trying to figure it out for a while.
It's not about inner class.
define function is a method and you should use it for an instance not static.
Below code works fine. because, we called define over an instance.
test.innerclass().defined()
test.innerclass() make an instance for test.innerclass class.
According to comments you want to get vara and varb
Try this.
obj = test.innerclass()
obj.defined()
print(obj.vara, obj.varb)

Class Solution - NameError: name 'Solution' is not defined

I tried all possible things to resolve this on compilation but still get this message. Is there anything wrong with class and function definition ?
class Solution:
def longestPalindrome(self, s):
s = 'tracecars'
print(str(Solution().longestPalindrome(s)))
PyCharm 2019.1.3 with Python 3.8
File "C:/Nityam/PythonProjects/DailyProblems/Palindrome.py", line 1,
in
class Solution:
File "C:/Nityam/PythonProjects/DailyProblems/Palindrome.py", line 7,
in Solution
print(str(Solution().longestPalindrome(s))) NameError: name 'Solution' is not defined
Process finished with exit code 1
First of all we can't reproduce your error.
Second one - your class method doesn't return anything.
Third - you don't create class object (it's OK - you can create it inline as you did, but why then do you need a class?)
Maybe you want something like this:
class Solution:
def longestPalindrome(self, s):
s = 'tracecars'
# some code here
return s
solution_obj = Solution()
print(str(solution_obj.longestPalindrome('somestring')))
also read about __init__ method for class - maybe you want s be a inner class variable (field)?

user defined class serialization and deserialization in python

I am very new to python : I want to serialize and deserialize my custom object in python. Please guide me on the same. I have a sample class :
import pickle
import json
class MyClass():
variable = "blah"
num = 10
def function(self):
print("this is a message inside the class.")
def get_variable():
return variable
def get_num():
return num
def main():
myObj = MyClass()
with open('/opt/infi/deeMyObj.txt', 'w') as output:
pickle.dump(myObj, output,pickle.HIGHEST_PROTOCOL)
with open('/opt/infi/deeMyObj.txt', 'r') as input:
myObjread = pickle.load(input)
print myObjread.get_variable()
print myObjread.get_num()
main()
I am getting following error :
Traceback (most recent call last):
File "sample.py", line 30, in
main()
File "sample.py", line 27, in main
print myObjread.get_variable()
TypeError: get_variable() takes no arguments (1 given)
Main intention is to read the object back.
To expand on jasonharper's comment, your get_variable and get_num methods aren't referring to the class's member variables. They should take the object as their first argument, e.g.
class MyClass:
...
def get_variable(self):
return self.variable
I think your serialization code is OK, but I might be wrong.
(Aside)
This is a bit off-topic, but another thing to note: when you define variables directly within the class block, they're defined on the class, not on objects of that class. That happens to work out in this case, since Python will look for a class-level variable of the same name if it can't find one on the object. However, if you store, say, a list in one of them and start modifying it, you'd end up sharing it between objects, which is probably not what you want. Instead you want to define them on in an __init__ method:
class MyClass:
def __init__(self):
self.variable = "blah"

Python : AttributeError

I get an AttributeError I can't seem to work out.
I'm working with two classes.
The first class goes something like that.
class Partie:
def __init__(self):
# deleted lines
self.interface = Interface(jeu=self)
def evaluerProposition(self):
# computations
self.interface.afficherReponse()
Introducing second class (in a separate file).
class Interface:
def __init__(self, jeu):
self.jeu = jeu
self.root = tkinter.Tk()
# stuff
def onClick(self, event):
# talk
self.jeu.evaluerProposition()
def afficherReponse(self):
# stuff
I start the whole thing by
partie = Partie()
All manipulations on my widget work fine until some click event causes
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeInterface.py", line 197, in clic
self.jeu.evaluerProposition()
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeJeu.py", line 55, in evaluerProposition
self.interface.afficherReponse()
AttributeError: 'Partie' object has no attribute 'interface'
I typed in the interpretor
>>> dir(partie)
and got a long list in return with 'interface' among the attributes.
Also typed
>>> partie.interface
<classeInterface.Interface object at 0x02C39E50>
so the attribute seems to exist.
Following the advice in some former post, I checked the instance names do not coincide with module names.
I am confused.
Most likely, in some code that you're not showing us, you're doing something like this:
self.some_button = tkinter.Button(..., command=self.interface.onClick())
Note the trailing () on onClick(). This would cause the onClick method to be called at the time the button is created, which is probably before your constructor is done constructing the instance of the Partie class.

How can I get child classes to use parent variables without redefining them?

Long time reader, first time asker. Anyway, Here's the code I'm working with:
class Person(object):
def __init__(self, s):
self.name = s
self.secret = 'I HAVE THE COOKIES'
#classmethod
def shout(self):
print self.name.upper()
class Kid(Person):
def __init__(self, s):
super(Kid,self).__init__(s)
self.age = 12
b = Person('Bob')
k = Kid('Bobby')
print b.name
print k.name
print k.age
print k.secret
k.shout()
Which results in this output and error:
Bob
Bobby
12
I HAVE THE COOKIES
Traceback (most recent call last):
File "a.py", line 22, in <module>
k.shout()
File "a.py", line 8, in shout
print self.name.upper()
AttributeError: type object 'Kid' has no attribute 'name'
I assumed that Kid would be able to use the Person's shout method substituting its (the kid's) "self" for parent (where the method lives). Apparently, that's not the case. I know I could declare name outside of init, but that's both unable to accomodate inputted data and a no-no. Another alternative would be to redefine shout for every child of Person, but that's a lot of repeated code that I'm trying to avoid.
Thanks very much in advance!
The issue is that #classmethod is a method on a class. It does not have access to an instance's attributes. Specifically the method is actually passed the class object, thus self is misnamed. You should really call shout's argument cls. If you remove the #classmethod then this would all make sense and your code would work as expected.
As it is, you can think of k.shout() as equivalent to Kid.shout().

Categories