[Python-Dev] Early PEP draft (For Python 3000?)
jcarlson at uci.edu
Wed Oct 19 20:03:13 CEST 2005
Jim Jewett <jimjjewett at gmail.com> wrote:
> (In http://mail.python.org/pipermail/python-dev/2005-October/057251.html)
> Eyal Lotem wrote:
> > Name: Attribute access for all namespaces ...
> > global x ; x = 1
> > Replaced by:
> > module.x = 1
> 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.
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().
Want to make that automatic? Write an import hook that puts a reference
to the module in the module itself on load/reload.
More information about the Python-Dev