base classes

Mats Wichmann mats at laplaza.org
Wed Aug 22 13:06:36 EDT 2001


On Wed, 22 Aug 2001 14:48:45 +0200, Fredrik Stenberg
<mail at fredriks.org> wrote:

:On Wed, 22 Aug 2001, Pete wrote:
:> 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 )
:>         a2.__init__( self )
:>
:> AA = aa()
:> print AA.__dict__
:> --------------------------------------
:> this code prints:
:> {'a': 2}


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

I guess we're all not sure what it is you want.  As you see, it's not
an /error/ for two base classes to provide the same attribute. What
you've said is that you want an instance of 'aa' to have an attribute
'a',  and so it does.  One is tempted to ask "what's the problem?".

As Fredrik pointed out, if you want the symbols provided by each base
class to automatically remain separate, you can use the name mangling
trick (preface the attribute name with double underscore), but then
you have to use the mangled names in the instance...



Mats Wichmann

(Anti-spam stuff: to reply remove the "xyz" from the
address xyzmats at laplaza.org. Not that it helps much...)



More information about the Python-list mailing list