On 18 Feb 2007 04:24:47 -0800, "Fuzzyman" <[EMAIL PROTECTED]> wrote:

>On Feb 17, 8:33 pm, deelan <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to