Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing __init__ and inheritance in python
    primarykey
    data
    text
    <p>I'm a little confused regarding using <code>__init__</code> and inheritance and would love to get some help with it.</p> <p>I have a class [that can not be changed]:</p> <pre><code>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 </code></pre> <p>this is some class that was written and I'm using it when overloading its functions:</p> <pre><code>class B(A): def func1(self) op3() op4() def func2(self) op5() </code></pre> <p>My problem starts here : I want to use Class B several times and sent each time different parameter:</p> <pre><code>---new .py file: --- def myFunction(): myInstance = B() </code></pre> <p>I want to do something like :</p> <pre><code>def myFunction(): myInstance = B(40,"test") </code></pre> <p>I thought adding to B class - <code>__init__</code> but the problem is that now I'm not sure what <code>self</code> it would use : explanation : Inside B class I have overloaded <code>func2(self)</code> I thought writing :</p> <pre><code>class B(A): def __init__(self, paramNumber, paramString) self.paramNumber = paramNumber self.paramString = paramString </code></pre> <p>but now in <code>func2(self)</code> , what <code>self</code> is used? the new one? the old one? will it work? meaning I will have : <code>self.x , self.y and self.paramNumber self.parmString ?</code>they are different "selfs"</p> <p>Editing : Should I use super as well?</p> <pre><code> 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" </code></pre> <p>is this the correct solution?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload