hidden attributes
Bengt Richter
bokr at oz.net
Tue Dec 17 19:51:26 EST 2002
On 18 Dec 2002 00:09:49 GMT, bokr at oz.net (Bengt Richter) wrote:
>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'
>
This seems to work more generally, so maybe it's a better guess:
>>> class C: pass
...
>>> class D(object): pass
...
>>> class E(type): pass
...
>>> [type(x).__getattribute__(x,'__name__') for x in (C,D,E)]
['C', 'D', 'E']
>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