473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to list the superclassesof an object

Hi,

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

TIA
Jul 18 '05 #1
5 1720
Fernando Rodriguez wrote:
Hi,

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

class A: pass .... class B: pass .... class C(A,B): pass .... x=C()
x.__class__.__bases__ (<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):
class C(object, A, B): pass .... x = C()
x.__class__.__mro__ (<class '__main__.C'>, <type 'object'>, <class __main__.A at 0x402db41c>,
<class __main__.B at 0x402db44c>)


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

Jul 18 '05 #2
On Wed, 05 Nov 2003 16:17:11 GMT, Alex Martelli <al***@aleax.it> wrote:

x.__class__.__bases__
(<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
Jul 18 '05 #3
Fernando Rodriguez wrote:
On Wed, 05 Nov 2003 16:17:11 GMT, Alex Martelli <al***@aleax.it> wrote:

> x.__class__.__bases__(<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?


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

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


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

Jul 18 '05 #4
Fernando Rodriguez <fr*@easyjob.net> wrote in message news:<ht********************************@4ax.com>. ..
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
Jul 18 '05 #5
Fernando Rodriguez <fr*@easyjob.net> wrote in message news:
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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
2
by: tamasu | last post by:
Hi, I am retrieving a selected item from a list/menu component on an HTML page. The said value is passed as a parameter to a method initiated through <jsp:useBean. I am aware that when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.