Finding the name of a class
Larry Bates
larry.bates at websafe.com
Tue Aug 1 15:21:27 EDT 2006
Dennis Lee Bieber wrote:
> On Tue, 01 Aug 2006 10:56:52 -0500, Kirk Strauser <kirk at strauser.com>
> declaimed the following in comp.lang.python:
>
>> Larry Bates wrote:
>>
>>> print print b.__class__.__name__ gives what you want
>> That doesn't seem to do it, though. Here's the result of importing a module
>> from my company's internally-developed library:
>>
>>>>> from Daycos.TableCopier.copyfro import StateProcessor
>>>>> print StateProcessor.__class__.__name__
>> type
>>
>> I'm looking for something that would print 'StateProcessor' but am not
>> having much luck.
>
> And what is "StateProcessor"
>
>>>> class SP(object):
> ... pass
> ...
>>>> print SP.__class__.__name__
> type
>
> Looks like it is, itself, the class, not something within the class.
>
>>>> s=SP()
>>>> print s.__class__.__name__
> SP
>>>> DP = SP
>>>> d = DP()
>>>> print DP.__class__.__name__
> type
>>>> print d.__class__.__name__
> SP
When I do this:
class foo(object):
pass
if __name__=="__main__":
a=foo()
print a.__class__.__name__
Note: You must do it on instance of the class not the class itself.
it prints 'foo' for me. Not exactly sure why you get something very
different. I've used this LOTS of times in my code, but I'll admit
mostly with old-style classes.
-Larry Bates
More information about the Python-list
mailing list