Python2.2 doesn't give members of a list

Guido van Rossum guido at python.org
Thu Aug 9 21:09:57 EDT 2001


> :"Tim Peters" <tim.one at home.com> wrote in message news:<mailman.997328534.2490.python-list at python.org>...
> :>
> :> The new scheme is better because more consistent and predictable, but the
> :> change in dir(builtin_object) is starting to look like it *may* be a bad
> :> incompatibility with 2.1.  Much as I hate doing it, for this reason I'm
> :> 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 boddie.net (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 laplaza.org (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)
    else:
        if hasattr(x, '__dict__'):
            d = x.__dict__.copy()
        else:
            d = {}
        classattrs(x.__class__, d)
    k = d.keys()
    k.sort()
    return k

def classattrs(c, d):
    d.update(c.__dict__)
    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: http://www.python.org/~guido/)



More information about the Python-list mailing list