Getting a class name
Daniel Klein
danielkleinad at gmail.com
Sun Feb 18 07:58:32 EST 2007
On 18 Feb 2007 04:24:47 -0800, "Fuzzyman" <fuzzyman at gmail.com> wrote:
>On Feb 17, 8:33 pm, deelan <g... at zzz.it> wrote:
>> Harlin Seritt wrote:
>> > Hi,
>>
>> > How does one get the name of a class from within the class code? I
>> > tried something like this as a guess:
>>
>> > self.__name__
>>
>> Get the class first, then inspect its name:
>>
>> >>> class Foo(object): pass
>> ...
>> >>> f = Foo()
>> >>> f.__class__.__name__
>> 'Foo'
>> >>>
>>
>
>
>Why is the __name__ attribute not available to the instance? Why don't
>normal lookup rules apply (meaning that a magic attribute will be
>looked up on the class for instances) ?
Good question!
>>> f = Foo()
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
>>> dir(f.__class__)
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
>>>
Where is '__name__' ?
Dan
More information about the Python-list
mailing list