473,804 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" ,
"introspect ion" 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(ob j)
(if it exists).

Thanks for the help.

-- C

Jul 19 '05 #1
11 1802
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" ,
"introspect ion" 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(ob j)
(if it exists).

Thanks for the help.

-- C

Jul 19 '05 #2
On Thu, 12 May 2005 23:30:21 GMT, Farshid Lashkari <la********@SPA Mworldviz.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" ,
"introspect ion" 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(ob j)
(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(Exc eption): 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(Exc eption): 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(Exc eption, 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_descript or:

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.c om> 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(Exc eption): 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(Exc eption, 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.c om> 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(Exc eption): 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(Exc eption, 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

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

Similar topics

7
2860
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 parse error when I do this:
3
2096
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 win32com yet. 1. How do I know that...
2
10702
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 = CreateObject("Excel.Application") objWorkBook = objExcel.Workbooks.Open(targetfilename) * targetfilename is a variable that stores the excel file name and path. In the error handler,
108
6477
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 describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
2
2467
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 is concerned. I am going to develop a accounting software that can work on the console and accessed through ssh from other terminals. I will like to use the curses or ncurses library for the menus and the input forms with add, save, delete,...
26
1840
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 = 80 a = coffer() if a.temp 60:
18
6789
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, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
3
1321
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 support for 'className' at some point in Geko's life time, but when ? Now the really horribly funny thing is when using getAttribute() and friends IE only works with 'className' where as FF only works with 'class'. Funny funny behaviour, I don't say.
275
12431
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
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.