0

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?

5
  • They're the same self, but you've forgotten to call A.__init__ to set up .x and .y Commented Dec 23, 2013 at 9:40
  • Don't call A.__init__ directly as Eric mentions. Use super. Commented Dec 23, 2013 at 9:42
  • so wait - I need to add to my "def __init__(self,paramNumber, paramString) after setting this init, also the super?? Commented Dec 23, 2013 at 9:45
  • 1
    All methods of an object have the same self. It doesn't matter which class the methods come from; there aren't an A self and a B self you have to distinguish between. Commented Dec 23, 2013 at 9:51
  • why? If I'm using init is B class with self.xx and self.yy then how can I still reach self.x and self.y [ from A] Commented Dec 23, 2013 at 11:50

1 Answer 1

3

So basically you want to run __init__ from parent class inside child:

class B(A):
    def __init__(self, paramNumber, paramString):
        super(B, self).__init__(param1=1, param2=2)
        # the other code goes here
Sign up to request clarification or add additional context in comments.

2 Comments

I first use super and then add seld.paramString and self.paramNumber? I edited my question to what I hope is correct - is it ?
@user1386966 The order depends. Just look at the code and figure it out. Sometimes you want to fire super at the begining, sometimes at the end. I suppose that in your case it will be at the begining.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.