how to dynamically generate __name__ for an object?
Ian Kelly
ian.g.kelly at gmail.com
Wed Aug 10 11:25:08 EDT 2011
On Wed, Aug 10, 2011 at 8:48 AM, Fuzzyman <fuzzyman at gmail.com> wrote:
> __name__ can be a descriptor, so you just need to write a descriptor
> that can be fetched from classes as well as instances.
>
> Here's an example with a property (instance only):
>
>>>> class Foo(object):
> ... @property
> ... def __name__(self):
> ... return 'bar'
> ...
>>>> Foo().__name__
> 'bar'
But:
>>> Foo.__name__
'Foo'
>>> repr(Foo())
'<__main__.Foo object at 0x00CAFFD0>'
>>> Foo.__dict__['__name__']
<property object at 0x00CBA6F0>
It seems that Foo.__name__ and Foo.__dict__['__name__'] are not the
same thing, and Python itself only uses the former.
More information about the Python-list
mailing list