I'm a little confused regarding using __init__ and inheritance and would love to get some help with it.
I have a class [that can not be changed]:
class A(object):
def __init__(self, param1 = 7, param2 = 10):
self.x = param1
self.y = param2
def func1(self):
op1()
op2()
def func2(self):
pass
this is some class that was written and I'm using it when overloading its functions:
class B(A):
def func1(self)
op3()
op4()
def func2(self)
op5()
My problem starts here : I want to use Class B several times and sent each time different parameter:
---new .py file: ---
def myFunction():
myInstance = B()
I want to do something like :
def myFunction():
myInstance = B(40,"test")
I thought adding to B class - __init__ but the problem is that now I'm not sure what self it would use :
explanation :
Inside B class I have overloaded func2(self)
I thought writing :
class B(A):
def __init__(self, paramNumber, paramString)
self.paramNumber = paramNumber
self.paramString = paramString
but now in func2(self) , what self is used? the new one? the old one? will it work?
meaning I will have : self.x , self.y and self.paramNumber self.parmString ?they are different "selfs"
Editing : Should I use super as well?
class B(A):
def __init__(self, paramNumber, paramString,param1, param2)
self.paramNumber = paramNumber
self.paramString = paramString
super(B,self).__init__(param1,param2)
def myFunction():
myInstance = B(40,"test")
// I will get : self.x = 7
// self.y= 10
// self.paramNumber=40
//self.paramString = "test"
is this the correct solution?
self, but you've forgotten to callA.__init__to set up.xand.yA.__init__directly as Eric mentions. Use super.self. It doesn't matter which class the methods come from; there aren't anAself and aBself you have to distinguish between.