[Python-Dev] Early PEP draft (For Python 3000?)

Josiah Carlson jcarlson at uci.edu
Thu Oct 20 18:48:43 CEST 2005


Jim Jewett <jimjjewett at gmail.com> wrote:
> I'll try to be more explicit; if Josiah and I are talking past each
> other, than the explanation was clearly not yet mature.
> 
> (In http://mail.python.org/pipermail/python-dev/2005-October/057251.html)
> Eyal Lotem suggested:
> 
> > Name: Attribute access for all namespaces ...
> 
> >       global x ; x = 1
> > Replaced by:
> >       module.x = 1
> 
> I responded:
> > Attribute access as an option would be nice, but might be slower.
> 
> > Also note that one common use for a __dict__ is that you don't
> > know what keys are available; meeting this use case with
> > attribute access would require some extra machinery, such as
> > an iterator over attributes.
> 
> Josiah Carlson responded
> (http://mail.python.org/pipermail/python-dev/2005-October/057451.html)
> 
> > This particular use case is easily handled.  Put the following
> > once at the top of the module...
> 
> > module = __import__(__name__)
> 
> > Then one can access (though perhaps not quickly) the module-level
> > variables for that module.  To access attributes, it is a quick scan
> > through module.__dict__, dir(), or vars().
> 
> My understanding of the request was that all namespaces --
> including those returned by globals() and locals() -- should
> be used with attribute access *instead* of __dict__ access.

Yeah, I missed the transition from arbitrary stack frame access to
strictly global and local scope attribute access.


> module.x is certainly nicer than module.__dict__['x']
> 
> Even with globals() and locals(), I usually *wish* I could
> use attribute access, to avoid creating a string when what
> I really want is a name.

Indeed.

> The catch is that sometimes I don't know the names in
> advance, and have to iterate over the dict -- as you
> suggested.  That works fine today; my question is what
> to do instead if __dict__ is unavailable.
> 
> Note that vars(obj) itself conceptually returns a NameSpace
> rather than a dict, so that isn't the answer.

>>> help(vars)
vars(...)
    vars([object]) -> dictionary

    Without arguments, equivalent to locals().
    With an argument, equivalent to object.__dict__.

When an object lacks a dictionary, dir() works just fine.

>>> help(dir)
Help on built-in function dir:

dir(...)
    dir([object]) -> list of strings

    Return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it:

    No argument:  the names in the current scope.
    Module object:  the module attributes.
    Type or class object:  its attributes, and recursively the attributes of
        its bases.
    Otherwise:  its attributes, its class's attributes, and recursively the
        attributes of its class's base classes.


> My inclination is to add an __iterattr__ that returns
> (attribute name, attribute value) pairs, and to make this the
> default iterator for NameSpace objects.

def __iterattr__(obj):
    for i in dir(obj):
        yield i, getattr(obj, i)


> Whether the good of
>   (1)  not needing to mess with __dict__, and
>   (2)  not needing to pretend that strings are names
> is enough to justify an extra magic method ... I'm not as sure.

I don't know, but leaning towards no; dir() works pretty well.  Yeah, you
have to use getattr(), but there are worse things.

 - Josiah



More information about the Python-Dev mailing list