[Python-ideas] local variable access : __getlocal__ and __setlocal__

Josiah Carlson jcarlson at uci.edu
Sun Feb 18 22:12:21 CET 2007

"Pierre-Yves Martin" <pym.aldebaran at gmail.com> wrote:
> Currently it's possible to customize attribute access via methods :
> __getattribute__(self, name)
> __getattr__(self, name)
> __setattr__(self, name, value)
> but it is not possible to customize local variable access. It would be
> useful for example to allow implementation of a local constant or any other
> on-access/on-modification behavior. The function should could be:

To offer the ability to control such access in a function-local scope
would result in significant slowdown during the execution of the
function body.  This issue would propagate everywhere, as with your
offered semantics, every name lookup would result in a function call
(except for the __getlocal__ and __setlocal__ lookups apparently), and
function calls in Python are significantly slower than dictionary
lookups (globals, etc.) or array lookups (function local accesses).

There are many other reasons to dislike your proposal, but in an attempt
to get something done today, I'll restrain myself.

> #Sample 2 : Constants

I believe this is the wrong approach.  If you want to define constants,
create a class with read-only properties.  Alternatively, recent Pythons
allow for passing a dict subclasses to the global and local dictionaries
in the exec statement which implement your constant definitions.  Both
options can offer read-only constants, are available *now*, and even
better, won't destroy the performance of Python.

If you couldn't guess, -1 .

 - Josiah

More information about the Python-ideas mailing list