On Feb 18, 1:22 pm, "Michele Simionato" <[EMAIL PROTECTED]> wrote: > On Feb 18, 1:24 pm, "Fuzzyman" <[EMAIL PROTECTED]> wrote: > > > 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) ? > > Because __name__ isn't really an attribute, it is a descriptor defined > on > the metaclass: > > >>> type(type.__dict__['__name__']) > > <type 'getset_descriptor'> > > Seehttp://users.rcn.com/python/download/Descriptor.htmfor a guide to > descriptors, and the papers by me and David Mertz for a guide to > metaclasses. >
Thanks, I'll do some more reading. I got as far as this on my own: My guess is that it is because magic attributes are looked up on the class. When you ask the *class* what its name is, the metaclass answers on its behalf. The class has no real ``__name__`` attribute, so when you ask the instance (which doesn't inherit from the metaclass) you get an ``AttributeError``. Is this right ? In which case, how does the metaclass (typically ``type`` which has many instance) know which answer to supply ? A simple test indicates that the name *is* looked up on the metaclass : .. raw:: html {+coloring} >>> class meta(type): ... __name__ = 'fish' ... >>> class Test(object): ... __metaclass__ = meta ... >>> Test.__name__ 'fish' {-coloring} So maybe ``__name__`` is a propery and ``type`` keeps a dictionary of instances to names, registered in ``type.__new__`` ? Fuzzyman http://www.voidspace.org.uk/python/articles.shtml > Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list