Python2.2 doesn't give members of a list

James_Althoff at James_Althoff at
Mon Aug 13 20:42:15 CEST 2001

I like it a lot!


                    Guido van Rossum                                                                               
                    <guido at>        To:     python-list at                                       
                    Sent by:                  cc:                                                                  
                    python-list-admin@        Subject:     Re: Python2.2 doesn't give members of a list            
                    08/09/01 06:09 PM                                                                              

> :"Tim Peters" < at> wrote in message
news:<mailman.997328534.2490.python-list at>...
> :>
> :> The new scheme is better because more consistent and predictable, but
> :> change in dir(builtin_object) is starting to look like it *may* be a
> :> incompatibility with 2.1.  Much as I hate doing it, for this reason
> :> going to look into hacking inconsistent surprises back into dir().
> :>
> :> although-nothing-can-make-everyone-happy-ly y'rs  - tim

> On 9 Aug 2001 08:58:32 -0700, paul at (Paul Boddie) wrote:
> :Absolutely not! I was initially surprised that dir(object) didn't give
> :the methods as well as the attributes, and disappointed that
> :dir(object.__class__) doesn't state the methods available on that
> :object.
> ....
> : Instead, that
> :is left up to the programmer, unless he/she decides upon using the new
> :introspection module(s).
> :
> :Perhaps there should be a well-known and widely published way of doing
> :this kind of thing, but dir is a well-known function. It's a shame
> :about dir([])...

xyzmats at (Mats Wichmann) writes:

> I think so too.  Introspection seems to me to be a feature that should
> be easier, rather than harder, to get to.  dir() has done part of the
> that job, albeit inconsistently (funny, it didn't seem inconsistent
> after I got used to it).  I'd be sad to have less information
> available to dir().   I should warn that I'm coming at this mostly
> from the idea of teaching Python, which is something I do (less often
> than I'd like, sadly).

Hm.  How about making dir() incompatible in the other direction, and
letting it return a sorted list of *all* attributes (that one can
reasonably deduce)?
Like this:

def dir(x):
    if isinstance(x, type):
        d = {}
        classattrs(x, d)
        if hasattr(x, '__dict__'):
            d = x.__dict__.copy()
            d = {}
        classattrs(x.__class__, d)
    k = d.keys()
    return k

def classattrs(c, d):
    for b in c.__bases__:
        classattrs(b, d)

>>> print dir([])
['__add__', '__class__', '__contains__', '__delitem__', '__eq__', '__ge__',
'__getattr__', '__getitem__', '__getslice__', '__gt__', '__iadd__', '
__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__',
'__new__', '__repr__', '__rmul__', '__setitem__', '__setslice__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Note that print dir(list) would print the same.  Just an idea...

--Guido van Rossum (home page:

More information about the Python-list mailing list