How to find the classname of an object? (was Python Documentation)
Christopher J. Bottaro
cjbottaro at alumni.cs.utexas.edu
Fri May 13 11:14:27 EDT 2005
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
More information about the Python-list
mailing list