A stupid mistake that I took me some time to realize why:
>>> class add: #here I’m creating the class
def add(self,x,y):
print (x+y)
#below I’m creating a sub/child class “Myadd” from the super/parent class (unwittingly, I might add, assuming that I can use it, or the parent class, for that matter, without creating an object (instance) first... poor fool)
>>> class Myadd(add):
pass #I passed here because I don’t want to
#alter an functions
>>> Myadd.add(4,5)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
Myadd.add(4,5)
TypeError: add() takes exactly 3 positional arguments (2 given)
OF COURSE THIS IS WRONG BECAUSE I'M TRYING TO APPLY THE METHOD TO THE CLASS. I SHOULD CREATE A AN OBJECT FIRST! IT IS TELLING ME THAT THE CLASS TAKE 3 ARGUMENTS, WHICH IS TRUE, AND THEY ARE (SELF,X,Y). THE MYSTERY CAN BE SOLVED THUS:
>>> ObjectMyadd=Myadd()
>>> ObjectMyadd.add(3,3)
6
note to my poor ass: you don’t need to make a sub class to create a method. just create an object from the first class. so the concept of super/sub class doesn’t really “enter into it”>>python slang
>>> class add: #here I’m creating the class
def add(self,x,y):
print (x+y)
#below I’m creating a sub/child class “Myadd” from the super/parent class (unwittingly, I might add, assuming that I can use it, or the parent class, for that matter, without creating an object (instance) first... poor fool)
>>> class Myadd(add):
pass #I passed here because I don’t want to
#alter an functions
>>> Myadd.add(4,5)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
Myadd.add(4,5)
TypeError: add() takes exactly 3 positional arguments (2 given)
OF COURSE THIS IS WRONG BECAUSE I'M TRYING TO APPLY THE METHOD TO THE CLASS. I SHOULD CREATE A AN OBJECT FIRST! IT IS TELLING ME THAT THE CLASS TAKE 3 ARGUMENTS, WHICH IS TRUE, AND THEY ARE (SELF,X,Y). THE MYSTERY CAN BE SOLVED THUS:
>>> ObjectMyadd=Myadd()
>>> ObjectMyadd.add(3,3)
6
note to my poor ass: you don’t need to make a sub class to create a method. just create an object from the first class. so the concept of super/sub class doesn’t really “enter into it”>>python slang
No comments:
Post a Comment