[Python-Dev] SWIG and rlcompleter

James Y Knight foom at fuhm.net
Thu Aug 18 01:23:47 CEST 2005

On Aug 17, 2005, at 2:55 PM, Timothy Fitz wrote:

> On 8/16/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
>> -0  The behavior of dir() already a bit magical.  Python is much  
>> simpler
>> to comprehend if we have direct relationships like dir() and vars()
>> corresponding as closely as possible to the object's dictionary.  If
>> someone injects non-strings into an attribute dictionary, why should
>> dir() hide that fact?
> Indeed, there seem to be two camps, those who want dir to reflect  
> __dict__
> and those who want dir to reflect attributes of an object. It seems to
> me that those who want dir to reflect __dict__ should just use
> __dict__ in the first place.
> However, in the case of dir handling non-strings, should dir handle
> non-valid identifiers as well, that is to say that while
> foo.__dict__[2] = ... is an obvious case what about foo.__dict__["1"]
> ?
> Right now the documentation says that it returns "attributes", and I
> would not consider non-strings to be attributes, so either the
> documentation or the implementation should rectify this disagreement.

I initially was going to say no, there's no reason to restrict your  
idea of "attributes" to be purely strings, because surely you could  
use non-strings as attributes if you wished to. But Python proves me  
 >>> class X: pass
 >>> X.__dict__[1] = 5
 >>> dir(X)
[1, '__doc__', '__module__']
 >>> getattr(X, 1)
TypeError: getattr(): attribute name must be string

If dir() is supposed to return the list of attributes, it does seem  
logical that it should be possible to pass those names into getattr.  
I think I'd actually call that a defect in getattr() that it doesn't  
allow non-string attributes, not a defect in dir(). Ooh...even more  
annoying, it doesn't even allow unicode attributes that use  
characters outside the default encoding (ASCII).

But either way, there's absolutely no reason to worry about the  
attribute string being a valid identifier. That's pretty much only a  
concern for tab-completion in python shells.


More information about the Python-Dev mailing list