Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to use <code>super</code> in <code>child</code> to call <code>parent.__init__</code> and <code>parent2._init__</code>, then both parent <code>__init__</code>s must also call <code>super</code>:</p> <pre><code>class parent(Base): def __init__(self,x=1,y=2): super(parent,self).__init__(x,y) class parent2(Base): def __init__(self,x=3,y=4): super(parent2,self).__init__(x,y) </code></pre> <p>See <a href="https://stackoverflow.com/q/5033903/190597">"Python super method and calling alternatives"</a> for more details on the sequence of calls to <code>__init__</code> caused by using <code>super</code>.</p> <hr> <pre><code>class Base(object): def __init__(self,*args): pass class parent(Base): var1=1 var2=2 def __init__(self,x=1,y=2): super(parent,self).__init__(x,y) self.var1=x self.var2=y class parent2(Base): var4=11 var5=12 def __init__(self,x=3,y=4): super(parent2,self).__init__(x,y) self.var4=x self.var5=y def parprint(self): print self.var4 print self.var5 class child(parent, parent2): var3=5 def __init__(self,x,y): super(child, self).__init__(x,y) childobject = child(9,10) print childobject.var1 print childobject.var2 print childobject.var3 childobject.parprint() </code></pre> <hr> <p>You might be wondering, "Why use <code>Base</code>?". If <code>parent</code> and <code>parent2</code> had inherited directly from <code>object</code>, then <code>super(parent2,self).__init__(x,y)</code> would call <code>object.__init__(x,y)</code>. That raises a <code>TypeError</code> since <code>object.__init__()</code> takes no parameters.</p> <p>To workaround this issue, you can make a class <code>Base</code> which accepts arguments to <code>__init__</code> but does not pass them on to <code>object.__init__</code>. With <code>parent</code> and <code>parent2</code> inheriting from <code>Base</code>, you avoid the <code>TypeError</code>.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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