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

Matt matthew_shomphe at countrywide.com
Fri May 13 12:37:07 EDT 2005


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




More information about the Python-list mailing list