Finding the name of a class

Bruno Desthuilliers onurb at xiludom.gro
Wed Aug 2 03:37:34 EDT 2006


Larry Bates wrote:
> Kirk Strauser wrote:
>> Given a class:
>>
>>>>> class foo(object):
>>>>>     pass
>> how can I find its name, such as:
>>
>>>>> b = foo
>>>>> print something(b)
>> 'foo'
>>
>> I'm writing a trace() decorator for the sake of practice, and am trying to
>> print the name of the class that a traced method belongs to.  This seems
>> like it should be easy, but I think I've been staring at the problem too
>> long.
> 
> print print b.__class__.__name__  gives what you want

Actually it should be b.__name__, since b refers to *class* foo.
b.__class__ is the metaclass (usually 'type' unless there's a custom
metaclass).



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list