This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dir(object) does not list __name__
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, fdrake, skip.montanaro, tkpmep
Priority: normal Keywords:

Created on 2004-05-01 11:39 by tkpmep, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg20660 - (view) Author: Thomas Philips (tkpmep) Date: 2004-05-01 11:39
dir(object) returns 
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce
_ex__', '__repr__', '__setattr__', '__str__']

The __name__ method is not reported
msg20661 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-05-02 01:02
Logged In: YES 
user_id=44345

Are you sure that the object has an actual __name__ attribute 
(and not something computed by a __getattr__ method)?

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', 
'__stderr__', '__stdin__', '__stdout__', '_getframe', 'api_version', 
'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 
'callstats', 'copyright', 'displayhook', 'exc_clear', 'exc_info', 
'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 
'exitfunc', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 
'getfilesystemencoding', 'getrecursionlimit', 'getrefcount', 
'hexversion', 'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 
'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 
'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 
'settrace', 'stderr', 'stdin', 'stdout', 'version', 'version_info', 
'warnoptions']
>>> sys.__name__
'sys'
msg20662 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-05-02 19:47
Logged In: YES 
user_id=44345

After a message from the submitter, it's apparent he was referring 
to class objects not showing '__name__' attributes in dir() output.  
This is a case of an attribute not being visible to dir() because it's 
not directly present in the object's __dict__ and is trapped at 
evaluation time by __getattr__().  Short of hacking dir() or adding 
a special attribute ("__attributes__"?) to objects which have 
__getattr__() methods I don't see a way around this problem.

Wasn't there discussion of such an attribute which would expose 
such dynamic attributes to dir()?  I don't see anything in the 
implementation of PyObject_Dir().
msg20663 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2004-05-12 02:55
Logged In: YES 
user_id=3066

I'm not convinced that attributes dynamically provided by
__getattr__() aren't actual attributes; it would be best if
dir() reported them if they're available via getattr(ob,
name).  Whether or not this is practical is another matter.

I've just closed documentation bug #952212, so at least the
presence of the __name__ attribute on types and classes is
mentioned somewhere.

I'm re-classifying this bug report, since the dynamic
behavior of dir() is not a documentation issue.
msg59196 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-04 00:40
__name__ comes from object's meta class type just as other attributes
like __bases__. It's a bit surprising at first.
History
Date User Action Args
2022-04-11 14:56:03adminsetgithub: 40205
2008-01-04 00:40:18christian.heimessetstatus: open -> closed
resolution: wont fix
messages: + msg59196
nosy: + christian.heimes
2004-05-01 11:39:06tkpmepcreate