Calling the parent class' __init__

Roeland Rengelink r.b.rigilink at cable.a2000.nl
Fri Oct 6 03:59:26 EDT 2000


Thomas Gagne wrote:
> 
> 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?
> 
> --
> .tom

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

b = B('Hi','there...')
b.do()
Hi there...

--

Roeland



More information about the Python-list mailing list