class declaration shortcut

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Mar 1 11:46:58 CET 2007


In <54nlkgF21ft8aU3 at mid.individual.net>, Bjoern Schliessmann wrote:

> Michele Simionato wrote:
>> On Mar 1, 9:40 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net>
>>> In <54msaoF21c6h... at mid.individual.net>, Bjoern Schliessmann
> 
>>>> But what's it (__name__) good for?
>>>
>>> As objects don't know to which name they are bound, that's a good
>>> way to give some information in stack traces or when doing
>>> introspection.
>> 
>> Also, the name is used by pickle to find the class of pickled
>> instances.
> 
> Mh. I suspect there's also more to it than I see now, but this
> __name__ seems quite useless to me. What if I rebind the class'
> name after definition?

Then it's a way to still get the name of the definition via `__name__`. 
Together with the name of the module where the definition took place which
is also available as attribute on the objects it is very useful to find
out where to look for the source code.  Just see the default `repr()` of
class objects.

> Or is it really just for some manual introspection? If it is, it seems a
> bit of an overkill to me.
>
>>>> class Spam(object):
> ...     pass
> ...
>>>> Ham = Spam
>>>> Spam = 0
>>>> test = Ham()
>>>> test.__class__
> <class '__main__.Spam'>

What would you expect this to look like if there weren't a __name__
attribute?

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list