hidden attributes
Bengt Richter
bokr at oz.net
Tue Dec 17 19:09:49 EST 2002
On 17 Dec 2002 11:04:30 -0800, mis6 at pitt.edu (Michele Simionato) wrote:
>I've just discovered that the attribute __name__ is hidden i.e.
>not shown by dir:
>
>>>> class C: pass
>...
>>>> C.__name__
>'C'
>>>> dir(C)
>['__doc__', '__module__'] # __name__ is not shown
>>>> help(dir)
>Help on built-in function dir:
>
>dir(...)
> dir([object]) -> list of strings
>
> Return an alphabetized list of names comprising (some of) the attributes
> ^^^^^^^
> of the given object, and of attributes reachable from it:
>
> No argument: the names in the current scope.
> Module object: the module attributes.
> Type or class object: its attributes, and recursively the attributes of
> its bases.
> Otherwise: its attributes, its class's attributes, and recursively the
> attributes of its class's base classes.
>
>Why __name__ is hidden and how do I discover if there other hidden
>attributes I don't know about ?
>
My guess would be that somehow the attribute access for '__name__'
finds its way internally to the value via something like
>>> import types
>>> class C: pass
...
>>> types.ClassType.__getattribute__(C, '__name__')
'C'
but that's a guess. One could check the code, I suppose. I wonder how hard it
would be to write a little Python tool to grep the C sources for hardcoded
attribute names. It might make an interesting listing. Maybe such a script exists?
Regards,
Bengt Richter
More information about the Python-list
mailing list