I have something like this:
class exampleClass(object): def doSomething(self,number): return number + 1 class exampleClass2(exampleClass): def callDefDoSomething(self): print exampleClass.doSomething(5) exampleClass2.callDefDoSomething()
TypeError: unbound method callDefDoSomething() must be called with exampleClass2 instance as first argument (got nothing instead)
I started to learn about objects in Python but i cant find solution for this :(
You need to create an instance of the class, i.e., an active object, to make things work:
class exampleClass(object): def doSomething(self,number): return number + 1 class exampleClass2(exampleClass): def __init__(self): self.member1 = exampleClass() def callDefDoSomething(self): print self.member1.doSomething(5) object2 = exampleClass2() object2.callDefDoSomething()
doSomething is a method of
exampleClass. Therefore, it has to be called for an instance of this class.
callDefDoSomething, you use
exampleClass, however, is not an instance of this class but the class itself. What you want to use here is
self refers to the instance of exampleClass2
, for whichcallDefDoSomethings
is invoked, which, due to inheritance, is an instance ofexampleClass`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!Donate Us With