473,407 Members | 2,312 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,407 software developers and data experts.

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 1771
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I have been trying to dynamically call a static member function, as follows: $className = 'MyClass'; $methodName = 'MyMethod' $result = $className::$methodName(); However, I get a...
3
by: T.T.H. | last post by:
Hi I am coding a C++ COM object which I want to access in Python. For that I do have some detail questions and need help since I am not that familiar with the documentation itself of Python and...
2
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel =...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: krishnakant Mane | last post by:
hello, I am a new member to this list. I am Krishnakant from India, Mumbai. I have been coding in python for quite some time and now I am at the intermediate level of programming as far as python...
26
by: momobear | last post by:
hi, I am puzzled about how to determine whether an object is initilized in one class, anyone could give me any instructions? here is an example code: class coffee: def boil(self): self.temp =...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
3
by: Aaron Gray | last post by:
Okay, onto className and the HTML class attribute. When was className introduced, I believe it was introduced by Microsoft, although I could be wrong. Now I also believe that Mozilla added...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.