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.

Title: dir() should only return strings
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, loewis, skip.montanaro, vstinner
Priority: normal Keywords: patch

Created on 2004-10-28 18:08 by skip.montanaro, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
dir_type.patch vstinner, 2009-02-16 17:34
Messages (5)
msg60592 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-10-28 18:08
Calling this a bug in Python is probably a misnomer.
Think of it as more of a "request for workaround".
Recent versions of SWIG create wrapper classes that
contain a Python class as a key in their __dict__'s.
Consequently, something like

    [k for k in dir(obj) if k.startswith("set_")]

because there is an element in dir(obj) that's not a
string or a unicode object.

I realize dir() is mostly a debugging aid, unlikely to be
used frequently in production code, but I'd like see the
docs updated so that it's guaranteed to only return
strings or unicode objects and I'd like its implementation
changed to enforce that (and maybe even warn if
something else is found).

If this is seen as an acceptable change I will work up a
msg60593 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2004-10-28 19:38
Logged In: YES 

It seems that the documentation guarantees that it returns
"names" ("the list contains the object's attributes' names,
..."). Most people certainly expect names to be strings
(although they might be Unicode objects at some point in the
future :-).

So I think it is ok to fix dir in this respect.
msg81448 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-09 06:55
Still present in trunk:69418:

class obj: pass

obj.__dict__[1] = 0
[k for k in dir(obj) if k.startswith("set_")]
msg82255 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-02-16 17:34
Yes, the issue still exists in 2009. Attached patch contains a test + 
a patch to test the type of each value of the dir() result.

I'm not sure that it's a good idea to apply my patch. It's maybe not 
the job of Python to check the types of dir() result.
msg83837 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-20 00:47
It's a feature request and it looks like except of Skip Montanaro in 
2004, nobody requires this feature. I don't like my patch because it 
slows down Python just for a very rare case (this issue).

I choose to close it. Reopen this issue if you think that this feature 
(check type of dir() result) is a must have ;-)
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 41095
2009-03-20 00:47:56vstinnersetstatus: open -> closed
resolution: wont fix
messages: + msg83837
2009-02-16 17:34:10vstinnersetfiles: + dir_type.patch
nosy: + vstinner
messages: + msg82255
keywords: + patch
2009-02-09 06:55:49ajaksu2setnosy: + ajaksu2
type: behavior
messages: + msg81448
versions: + Python 2.7
2004-10-28 18:08:16montanaro.historiccreate