__name__
Joshua Macy
amused at webamused.com
Wed Jan 19 22:07:26 EST 2000
JB wrote:
>
> Could anyone tell me how to get a classes name:
>
> class MyClass:
> def className( self ):
> return self.__name__
>
> This doesn't fly. The docs say a class has the __name__ attribute but...
>
Class has a __name__ attribute, but in your example, self is an
instantiated object
of type MyClass, not a class. To get the __name__ attribute you either
have to refer to it directly
(Class.__name__), or indirectly via the __class__ attribute of the
object (object.__class__.__name__)
E.g.
>>> class MyClass:
... def __init__(self):
... print self.__class__.__name__
...
>>> m = MyClass()
MyClass
>>> print MyClass.__name__
MyClass
>>> print m.__class__.__name__
MyClass
Joshua
More information about the Python-list
mailing list