Calling the parent class' __init__

Vetle Roeim vr at acm.org
Fri Oct 6 07:43:06 EDT 2000


* Roeland Rengelink

> > A parent class defines some instance variables I want to access in
> > the subclass. I thought I was supposed to call the super class'
> > __init__ but whenever I tried I got errors so I didn't--but the
> > first time I tried accessing the super's instance variable (oddly
> > enough, in one of the super's methods) I got a name error. Wierd.
> > 
> > Does anyone have some pointers?
> 
> You'll need something like:
> 
> class A:
>     def __init__(self, value):
>         self.a = value
> 
> class B(A):		
>     def __init__(self, val1, val2):
>         A.__init__(self, val1)
>         self.b = val2
> 
>     def do(self)
>         print self.a, self.b

or:
  class C(A):
      def __init__(self, val1, **keywordvals):
          apply(A.__init__, (self, val1), keywordvals)


the-possibilities-are-endless-ly y'rs, vr







More information about the Python-list mailing list