base classes

Hans Nowak hnowak at cuci.nl
Wed Aug 22 10:09:41 EDT 2001


>===== Original Message From "Pete" <pete at ipartners.pl> =====
>One question about classes. I have two classes, both have attribute called
>'a'
>class a1:
>    def __init__( self ):
>        self.a = 1
>class a2:
>    def __init__( self ):
>        self.a = 2
>class aa( a1, a2 ):
>    def __init__( self ):
>        a1.__init__( self )

Note that this creates a in the namespace of this instance, with value 1... 
(In other words, inspecting self.a would now yield the value 1.)

>        a2.__init__( self )

...and this re-creates it, overwriting the old value. The result is:

>AA = aa()
>print AA.__dict__
>--------------------------------------
>this code prints:
>{'a': 2}

>The question is: cannot 2 base classes have the same attributes? What is
>workaround?

If you want to work around it (and thus use both values defined by a1 and a2), 
try renaming the first a after a1.__init__, e.g.:

    def __init__( self ):
        a1.__init__( self )
        self.a1_a = self.a
        a2.__init__( self )
        print self.a1_a, self.a  # should print 1 and 2

HTH,

--Hans Nowak





More information about the Python-list mailing list