Fernando Rodriguez wrote:
[color=blue]
> Hi,
>
> How can I list the superclasses of an object? O:-)[/color]
[color=blue][color=green][color=darkred]
>>> class A: pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> class B: pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> class C(A,B): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> x=C()
>>> x.__class__.__b ases__[/color][/color][/color]
(<class __main__.A at 0x402db41c>, <class __main__.B at 0x402db44c>)
You may need a recursive walk up the (DA) graph if you also want
bases of bases, etc, among 'superclasses'; alternatively, but
ONLY for newstyle classes (recommended anyway for many reasons):
[color=blue][color=green][color=darkred]
>>> class C(object, A, B): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> x = C()
>>> x.__class__.__m ro__[/color][/color][/color]
(<class '__main__.C'>, <type 'object'>, <class __main__.A at 0x402db41c>,
<class __main__.B at 0x402db44c>)[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
the __mro__ attribute of a newstyle class does the walk on your
behalf, in the right order, removing duplicates, etc, etc...
On Wed, 05 Nov 2003 16:17:11 GMT, Alex Martelli <aleax@aleax.it > wrote:
[color=blue][color=green][color=darkred]
>>>> x.__class__.__b ases__[/color][/color]
>(<class __main__.A at 0x402db41c>, <class __main__.B at 0x402db44c>)[/color]
I didn't know the existence of the __bases__ attribute, and it doesn't show
with dir(). How can I get a list of ALL the attributes of an object?
I thought that dir() listed every attribute.... O:-)
[color=blue]
>
>You may need a recursive walk up the (DA) graph if you also want
>bases of bases, etc, among 'superclasses'; alternatively, but
>ONLY for newstyle classes (recommended anyway for many reasons):[/color]
I haven't used python in a while and all my classes are 'old style'. I'd like
to get up to date. Where can I find info about the differences / advantages of
these new classes? Is it safe to convert all my previous classes to new ones,
and how can I do it? O:-)
Fernando Rodriguez wrote:
[color=blue]
> On Wed, 05 Nov 2003 16:17:11 GMT, Alex Martelli <aleax@aleax.it > wrote:
>
>[color=green][color=darkred]
>>>>> x.__class__.__b ases__[/color]
>>(<class __main__.A at 0x402db41c>, <class __main__.B at 0x402db44c>)[/color]
>
> I didn't know the existence of the __bases__ attribute, and it doesn't
> show
> with dir(). How can I get a list of ALL the attributes of an object?[/color]
try hasattr(x, somest) for all identifier strings somest (up to whatever
length you're comfortable with). Nothing stops an object from 'inventing'
attributes on the fly when queried about them, e.g:
class allem(object):
def __getattr__(sel f, name): return name
x=allem()
now x 'has' ANY attribute you can name, in the sense it will give a
value for x.supercalifrag ilisticexpialid ocious and so on. How else
save by exhaustive search could you find this out...?
[color=blue]
> I thought that dir() listed every attribute.... O:-)[/color]
No, it can't take days every time you call it;-)
[color=blue][color=green]
>>You may need a recursive walk up the (DA) graph if you also want
>>bases of bases, etc, among 'superclasses'; alternatively, but
>>ONLY for newstyle classes (recommended anyway for many reasons):[/color]
>
> I haven't used python in a while and all my classes are 'old style'. I'd
> like to get up to date. Where can I find info about the differences /
> advantages of
> these new classes? Is it safe to convert all my previous classes to new
> ones, and how can I do it? O:-)[/color]
I suggest peeking at the OO chapter of Python in a Nutshell -- I
think I cover the issues decently (do it for free by subscribing
at safari.oreilly. com and canceling before 14 days, since the
first 2 weeks are free).
Fernando Rodriguez <frr@easyjob.ne t> wrote in message news:<ht6iqv8l8 vockj88t4ffs6k7 rnplbujlqm@4ax. com>...[color=blue]
> Hi,
>
> How can I list the superclasses of an object? O:-)
>
> TIA[/color]
Take a look at self.__class__. __bases__ from within one of your methods.
Fernando Rodriguez <frr@easyjob.ne t> wrote in message news:[color=blue]
> Where can I find info about the differences / advantages of
> these new classes?[/color]
Comment