On Thu, 01 Mar 2007 10:44:48 +0100, Bjoern Schliessmann wrote: > 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? Or is it really just for some manual > introspection? If it is, it seems a bit of an overkill to me.
Overkill? Storage of a single attribute holding a (usually short) string is overkill? The thing to remember is that the link between names and objects in Python is one-way only. Names (by definition) have to know which object they are bound to. But objects don't know what name, or names, (if any) are bound to themselves. Yes, they *could* keep a list of the names bound to them, but that would be a lot of work at runtime for little benefit. So if you rebind a name, the objects can't tell. And generally you wouldn't want them to. Something like the following is rightfully quite rare: >>> int <type 'int'> >>> int.__name__ 'int' >>> foo = int; int = "something else" >>> type(0) # Do you expect <type 'foo'>? <type 'int'> >>> int 'something else' Normally the reason for rebinding classes, functions etc. are (1) to save typing and (2) an optimization to save name look-ups. # slow way for item in sequence: package.module.function(item) # faster way f = package.module.function for item in sequence: f(item) When you do that, you wouldn't expect the __name__ of some.module.function to change to f, and it doesn't. The downside is that on the rare corner case where you do want the __name__ of an object to change, it doesn't. -- Steven D'Aprano -- http://mail.python.org/mailman/listinfo/python-list