base classes

Karthik Gurumurthy karthikg at aztec.soft.net
Wed Aug 22 09:37:11 EDT 2001


It did'nt work that way for me.
Infact i observed that it depends on
which base class's __init__ is called first for instance data attributes.

But it works the other way for instance method attributes (ie as per the
rules stated)
Can someone make this clear?

thanks,,
karthik.

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Doug Fort
Sent: Wednesday, August 22, 2001 6:10 PM
To: python-list at python.org
Subject: Re: base classes


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#SECTION0011510000000000000
000

<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

--
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list