base classes
Doug Fort
doug.fort at verizon.net
Wed Aug 22 08:40:29 EDT 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}
>
> but if I change self.a to self.a_ in class a1:
> 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__
> ------------------------------------------
> I got:
> {'a_': 1, 'a': 2}
>
> The question is: cannot 2 base classes have the same attributes? What is
> workaround?
>
> Pete
>
>
>
Check out the Python Tutorial
http://www.python.org/doc/current/tut/node11.html#SECTION0011510000000000000000
<quote>
The only rule necessary to explain the semantics is the resolution rule
used for class attribute references. This is depth-first, left-to-right.
Thus, if an attribute is not found in DerivedClassName, it is searched in
Base1, then (recursively) in the base classes of Base1, and only if it is
not found there, it is searched in Base2, and so on.
</quote>
If you defined class aa as aa(a2, a1), you would find the other value.
--
Doug Fort <dougfort at dougfort.net>
http://www.dougfort.net
______________________________________________________________________
Posted Via Uncensored-News.Com - Still Only $9.95 - http://www.uncensored-news.com
With Seven Servers In California And Texas - The Worlds Uncensored News Source
More information about the Python-list
mailing list