class declaration shortcut

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Fri Mar 2 15:28:06 EST 2007


Arnaud Delobelle wrote:

> Don't see it as the first name a class is bound to, but rather as
> the name a class is defined as.
> If class_object.__name__ == 'Foo' it means that somewhere in your
> code there is a class definition:
> 
> class Foo:
>     # stuff
> 
> Same for function: if function_object.__name__ == 'bar' it means
> that somewhere you have
> 
> def bar(...):
>     # stuff
> 
> (Of course this is not the case if you use another way to define
> functions or classes, e.g. type() )

This is somehow contrary to my understanding of the Python names
concept.

What if I use a loop to define several classes based on data --
they'll all have the same __name__ unless I change it manually.

Having this __name__ attribute set seems to me like "magic behind
the lines" which Python strives to evade, doesn't it? Personally,
I'd prefer inserting a mechanism for this manually if and when I
really need the functionality, but see below.

> What I described above is quite useful I think.  The alternative
> (anonymous classes) is that given an object myobj you have no
> means to find out what its class is (by that I mean to be able to
> locate the class definition in your source code), apart from
> stabbing in the dark (i.e. trying type(myobj)==someclass until
> successful).

In the typical case where you have one name per class definition,
yes.

Perhaps I'm lacking a typical application of __name__; that must be
why I'm so stubborn here ;)

Regards,


Björn

-- 
BOFH excuse #419:

Repeated reboots of the system failed to solve problem




More information about the Python-list mailing list