Finding the name of a class
Shane Hathaway
shane at hathawaymix.org
Thu Aug 3 12:39:19 EDT 2006
John Salerno wrote:
> Shane Hathaway wrote:
>
>> Don't forget to file a bug.
>
> I'm reluctant to call it a bug just yet. Here's more stuff below.
> There's obviously a difference between old- and new-style classes. It
> seems that as far as new-style is concerned, __name__ is an attribute of
> __class__ (along with a bunch of other stuff), but not of Foo itself.
I'm not sure what you're saying. The class of a class is the 'type'
builtin, unless metaclasses are involved. So your expression
"dir(Foo.__class__)" is equivalent to "dir(type)", and the 'type'
builtin happens to have a __name__ attribute that dir() notices. Take a
look:
>>> class Foo(object):
... pass
...
>>> Foo.__class__ is type
True
>>> Foo.__name__
'Foo'
>>> Foo.__class__.__name__
'type'
The bug is that the expression "dir(someclass)", where the class is a
user-defined class of either new or old style, never reveals to the user
that the class object has a __name__ attribute. I tested this with
Python versions 2.3 through 2.5b1. This is an education issue; since
that important attribute is not in the list, newcomers are not likely to
discover it, and may instead use strange incantations to get the name of
a class.
Do you want me to file the bug?
Shane
More information about the Python-list
mailing list