Finding the name of a class
Tim Chase
python.list at tim.thechases.com
Tue Aug 1 12:13:30 EDT 2006
>> While this is surely true, would somebody explain why I had such trouble
>> finding this?
>
> I think __name__ is an attribute of the class itself, not the instance:
That makes sense, but what doesn't make sense is why, when you do
a dir(Foo), you don't get '__name__' in the returned list of
available things Python knows about a Foo.
>>> class Foo(object):
... pass
...
>>> myClass = Foo
>>> myInstance = Foo()
>>> # does myClass have a '__name__' attribute?
>>> '__name__' in dir(myClass)
False
>>> # that's a negative, buster
>>> '__name__' in dir(myInstance)
False
>>> # haha, just kidding, it really did have a __name__
>>> # proof that dir() isn't showing everything:
>>> myClass.__name__
'Foo'
>>> myInstance.__name__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Foo' object has no attribute '__name__'
It's the
>>> '__name__' in dir(myClass)
False
>>> myClass.__name__
'Foo'
that throws me. What other super-secret tricks have I missed
because dir() didn't tell me about them?
-a still-confused tkc
More information about the Python-list
mailing list