469,336 Members | 5,233 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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

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

Jul 19 '05 #1
11 1649
This will get the name of an objects class

obj.__class__.__name__

This will return a tuple of its base classes

obj.__class__.__bases__

Christopher J. Bottaro wrote:
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

Jul 19 '05 #2
On Thu, 12 May 2005 23:30:21 GMT, Farshid Lashkari <la********@SPAMworldviz.com> wrote:
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.,
class C(object): pass ... class B1(C): pass ... class B2(C): pass ... class A(B1, B2): pass ... obj = A()
obj.__class__.__name__ 'A' obj.__class__.__bases__ (<class '__main__.B1'>, <class '__main__.B2'>)
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')

Christopher J. Bottaro wrote:
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


Regards,
Bengt Richter
Jul 19 '05 #3
On Friday 13 May 2005 03:11, Bengt Richter wrote:
>>> type(obj).mro()


[<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.
Jul 19 '05 #4
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. 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

Jul 19 '05 #5
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?

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

Jul 19 '05 #6

Christopher J. Bottaro wrote:
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?

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']

Jul 19 '05 #7
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. 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/?grou...70&atid=355470

STeVe
Jul 19 '05 #8
On 13 May 2005 09:37:07 -0700, "Matt" <ma*************@countrywide.com> wrote:

Christopher J. Bottaro wrote:
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
Jul 19 '05 #9

Bengt Richter wrote:
On 13 May 2005 09:37:07 -0700, "Matt" <ma*************@countrywide.com> wrote:

Christopher J. Bottaro wrote:
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?

Jul 19 '05 #10
On 13 May 2005 14:59:13 -0700, "Matt" <ma*************@countrywide.com> wrote:

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/pyt...ry/051098.html

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

Regards,
Bengt Richter
Jul 19 '05 #11

Bengt Richter wrote:
On 13 May 2005 14:59:13 -0700, "Matt" <ma*************@countrywide.com> wrote:

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

implementationof the old, sorry ;-/

At least that's the way it looks to me, without digging in that
partof 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/pyt...ry/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...70&atid=305470
(although from the thread you found, it appears to be somewhat
temporary in case everything starts breaking :-) )

Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Joshua Beall | last post: by
108 posts views Thread by Bryan Olson | last post: by
2 posts views Thread by krishnakant Mane | last post: by
18 posts views Thread by Gabriel Rossetti | last post: by
3 posts views Thread by Aaron Gray | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.