How to list the superclassesof an object

  • Thread starter Fernando Rodriguez
  • Start date
A

Alex Martelli

Fernando said:
Hi,

How can I list the superclasses of an object? O:)
(<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):
[/QUOTE]

the __mro__ attribute of a newstyle class does the walk on your
behalf, in the right order, removing duplicates, etc, etc...


Alex
 
F

Fernando Rodriguez

(<class __main__.A at 0x402db41c>, <class __main__.B at 0x402db44c>)

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:)
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):

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:)

TIA
 
A

Alex Martelli

Fernando said:
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?

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__(self, name): return name
x=allem()

now x 'has' ANY attribute you can name, in the sense it will give a
value for x.supercalifragilisticexpialidocious and so on. How else
save by exhaustive search could you find this out...?
I thought that dir() listed every attribute.... O:)

No, it can't take days every time you call it;-)

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:)

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).


Alex
 
J

Jeremy Fincher

Fernando Rodriguez said:
Hi,

How can I list the superclasses of an object? O:)

TIA

Take a look at self.__class__.__bases__ from within one of your methods.

Jeremy
 
M

Michele Simionato

Fernando Rodriguez said:
Where can I find info about the differences / advantages of
these new classes?

Alex Martelli's book and Guido's essay:
http://www.python.org/2.2.2/descrintro.html
Is it safe to convert all my previous classes to new ones,

Essentially yes.
and how can I do it? O:)

Put the line ``__metaclass__=type`` on top of your script. All your
old style class will be automagically converted to new style classes.

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top