How to find the classname of an object? (was Python Documentation)

  • Thread starter Christopher J. Bottaro
  • Start date
C

Christopher J. Bottaro

I actually want all the parent classes too. So if D derives off C derives
off B derives off A, I ultimately want a tuple ('D', 'C', 'B', 'A').

For those of you following the Python Documentation thread, this is a good
example of how the PHP manual is "better". I found how to do this in a few
seconds in PHP. I searched the Python docs for "class name", "classname",
"introspection" and "getclass". I looked in the Class section of the
tutorial also and also the Programming FAQ. The "related functions"
section of the PHP manual is really helpful. It would be cool if in the
section for the built-in function isinstance() or issubclass() there is a
section for "related functions" that would point me to getclassname(obj)
(if it exists).

Thanks for the help.

-- C
 
F

Farshid Lashkari

This will get the name of an objects class

obj.__class__.__name__

This will return a tuple of its base classes

obj.__class__.__bases__
 
B

Bengt Richter

This will get the name of an objects class

obj.__class__.__name__

This will return a tuple of its base classes

obj.__class__.__bases__

But not all base classes that it inherits from, e.g.,
('A', 'B1', 'B2', 'C', 'object')



Regards,
Bengt Richter
 
H

Heiko Wundram

[<class '__main__.A'>, <class '__main__.B1'>, <class '__main__.B2'>,
<class '__main__.C'>, <type 'object'>]

Wow! No need to write a depth-first tree-traversal algorithm... Somebody add
this idiom to the cookbook.

--- Heiko.
 
C

Christopher J. Bottaro

Bengt said:
[QUOTE= said:
tuple(x.__name__ for x in type(obj).mro())
('A', 'B1', 'B2', 'C', 'object')[/QUOTE]

Wow awesome, thats exactly what I was looking for. I hate to bring up the
documentation thing again...but.....where the hell is this in the
documentation? I looked under built-in function at type(), but it doesn't
give any info or links on the "type object".

Thanks.

--C
 
C

Christopher J. Bottaro

Christopher said:
Wow awesome, thats exactly what I was looking for.

Wait a sec...why doesn't the following code work then?

class FWException(Exception): pass
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro() ]

Thanks again.
--C
 
M

Matt

Christopher said:
Christopher said:
Bengt said:
type(obj)
<class '__main__.A'>
type(obj).mro()
[<class '__main__.A'>, <class '__main__.B1'>, <class '__main__.B2'>,
[<class '__main__.C'>, <type 'object'>]
tuple(x.__name__ for x in type(obj).mro())
('A', 'B1', 'B2', 'C', 'object')

Wow awesome, thats exactly what I was looking for.

Wait a sec...why doesn't the following code work then?

class FWException(Exception): pass
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro() ]

Thanks again.
--C


Is it because you need to inherit from "object"?

class FWException(Exception, object): pass # note "object"
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro()]

#prints ['FWC', 'FWB', 'FWA', 'FWException', 'Exception', 'object']
 
S

Steven Bethard

Christopher said:
Bengt Richter wrote:



Wow awesome, thats exactly what I was looking for. I hate to bring up the
documentation thing again...but.....where the hell is this in the
documentation?

py> help(type)
Help on class type in module __builtin__:

class type(object)
| type(object) -> the object's type
| type(name, bases, dict) -> a new type
....
| mro(...)
| mro() -> list
| return a type's method resolution order
....

Or even:

py> help(type.mro)
Help on method_descriptor:

mro(...)
mro() -> list
return a type's method resolution order

But yeah, I couldn't find it in the docs either. Please file a
documentation feature request:

http://sourceforge.net/tracker/?group_id=5470&atid=355470

STeVe
 
B

Bengt Richter

Christopher said:
Bengt Richter wrote:

type(obj)
<class '__main__.A'>
type(obj).mro()
[<class '__main__.A'>, <class '__main__.B1'>, <class '__main__.B2'>,
[<class '__main__.C'>, <type 'object'>]
tuple(x.__name__ for x in type(obj).mro())
('A', 'B1', 'B2', 'C', 'object')

Wow awesome, thats exactly what I was looking for.

Wait a sec...why doesn't the following code work then?
Well, I suspect it actually does work, technically, but I suspect
it hints at the way old-style classes were implemented in the environment
of the new, rather than giving you what you might expect.
class FWException(Exception): pass
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro() ]

Thanks again.
--C


Is it because you need to inherit from "object"?

class FWException(Exception, object): pass # note "object"
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro()]

#prints ['FWC', 'FWB', 'FWA', 'FWException', 'Exception', 'object']
I'm afraid inheriting explicitly from object will make the exception unraisable.
Exceptions are still based on "classic" classes for some reason that
I don't know enough about to explain.

So if you were hoping to use .mro() with old-style classes to see the
old-style inheritance chain, as opposed to new-style inheritance that
underlies access to special entities involved in the implementation of the old, sorry ;-/

At least that's the way it looks to me, without digging in that part of the code.

Regards,
Bengt Richter
 
M

Matt

Bengt said:
Christopher J. Bottaro wrote:

Bengt Richter wrote:

type(obj)
<class '__main__.A'>
type(obj).mro()
[<class '__main__.A'>, <class '__main__.B1'>, <class '__main__.B2'>,
[<class '__main__.C'>, <type 'object'>]
tuple(x.__name__ for x in type(obj).mro())
('A', 'B1', 'B2', 'C', 'object')

Wow awesome, thats exactly what I was looking for.

Wait a sec...why doesn't the following code work then?
Well, I suspect it actually does work, technically, but I suspect
it hints at the way old-style classes were implemented in the environment
of the new, rather than giving you what you might expect.
class FWException(Exception): pass
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro() ]

Thanks again.
--C


Is it because you need to inherit from "object"?

class FWException(Exception, object): pass # note "object"
class FWA(FWException): pass
class FWB(FWA): pass
class FWC(FWB): pass
e = FWC()
print [ cl.__name__ for cl in type(e).mro()]

#prints ['FWC', 'FWB', 'FWA', 'FWException', 'Exception', 'object']
I'm afraid inheriting explicitly from object will make the exception unraisable.
Exceptions are still based on "classic" classes for some reason that
I don't know enough about to explain.

So if you were hoping to use .mro() with old-style classes to see the
old-style inheritance chain, as opposed to new-style inheritance that
underlies access to special entities involved in the implementation of the old, sorry ;-/

At least that's the way it looks to me, without digging in that part of the code.

Regards,
Bengt Richter

D'oh! So I tested the .mro() functionality but not the
Exception-raisableness (?).

It seems unintuitive to me as to why inheriting from "object" would
prevent something that also inherited from "Exception" from being
raised. Does anyone have insight into why this happens?
 
B

Bengt Richter

Bengt Richter wrote: [...]
I'm afraid inheriting explicitly from object will make the exception unraisable.
Exceptions are still based on "classic" classes for some reason that
I don't know enough about to explain.

So if you were hoping to use .mro() with old-style classes to see the
old-style inheritance chain, as opposed to new-style inheritance that
underlies access to special entities involved in the implementation of the old, sorry ;-/

At least that's the way it looks to me, without digging in that part of the code.

Regards,
Bengt Richter

D'oh! So I tested the .mro() functionality but not the
Exception-raisableness (?).

It seems unintuitive to me as to why inheriting from "object" would
prevent something that also inherited from "Exception" from being
raised. Does anyone have insight into why this happens?
I googled for some discussion and found something straight from the BDFL:

http://mail.python.org/pipermail/python-dev/2005-January/051098.html

with a lot of interesting followup. Don't know what the status of the
patch is now.

Regards,
Bengt Richter
 
M

Matt

Bengt said:
Bengt Richter wrote: [...]
I'm afraid inheriting explicitly from object will make the
exception
unraisable.
Exceptions are still based on "classic" classes for some reason that
I don't know enough about to explain.

So if you were hoping to use .mro() with old-style classes to see the
old-style inheritance chain, as opposed to new-style inheritance that
underlies access to special entities involved in the
implementation
of the old, sorry ;-/
At least that's the way it looks to me, without digging in that
part
of the code.
Regards,
Bengt Richter

D'oh! So I tested the .mro() functionality but not the
Exception-raisableness (?).

It seems unintuitive to me as to why inheriting from "object" would
prevent something that also inherited from "Exception" from being
raised. Does anyone have insight into why this happens?
I googled for some discussion and found something straight from the BDFL:http://mail.python.org/pipermail/python-dev/2005-January/051098.html

with a lot of interesting followup. Don't know what the status of the
patch is now.

Regards,
Bengt Richter

Great googling! Reading through to the end it seems that there will be
a patch for 2.5:
http://sourceforge.net/tracker/index.php?func=detail&aid=1104669&group_id=5470&atid=305470
(although from the thread you found, it appears to be somewhat
temporary in case everything starts breaking :) )
 

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
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top