local variable access : __getlocal__ and __setlocal__

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: __getlocal__(name) __setlocal__(name, value) By default they are just access to the local dictionnary for reading and writting (AFAIK the only current way to access this dictionnary is using the locals() built-in). Here is a sample code of typical usage : ####################### #Sample 1 : Tracing local access old_getlocal = __getlocal__ old_setlocal = __setlocal__ #redefine the built-ins def __getlocal__(name): print 'accessing %s' % name return old_getlocal(name) def __setlocal__(name, value): print 'setting %s with value %s' % (name, value) old_setlocal(name, value) a = 1 b = 2 c = 3 #setting b and accessing a b = a + 1 ################### #Sample 2 : Constants old_getlocal = __getlocal__ old_setlocal = __setlocal__ class ConstantError(Exception):pass constants = [] #redefine the built-ins def __setlocal__(name, value): if old_getlocal(name, value) in constants: raise ConstantError() else: old_setlocal(name, value) a = 1 b = 2 c = 3 constants = [b, c] a = 4 #OK b = 5 #IMPOSSIBLE constant.remove(c) c = 6 #OK I found such approach more flexible than introducing a 'const' in python (not a good thing I think). And it enable many other customizations... Note : the fact that in my example I DO NOT access to any kind of writable local dictionary! This on purpose, the only way to access this dictionary should be : locals(), __getlocal__() and __setlocal__() built-ins. Pierre-Yves Martin pym<dot>aldebaran< a t >gmail<dot>com

"Pierre-Yves Martin" <pym.aldebaran@gmail.com> wrote:
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 [snip]
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

On 2/18/07, Pierre-Yves Martin <pym.aldebaran@gmail.com> wrote:
-1 IMO this would allow changing the basic behavior of Python too much. Variable assignment should be variable assignment. I am generally wary of such behind-the-scenes magic, since it can easily be taken too far, resulting in highly unreadable code -- not very Pythonic. - Tal Einat

"Pierre-Yves Martin" <pym.aldebaran@gmail.com> wrote:
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 [snip]
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

On 2/18/07, Pierre-Yves Martin <pym.aldebaran@gmail.com> wrote:
-1 IMO this would allow changing the basic behavior of Python too much. Variable assignment should be variable assignment. I am generally wary of such behind-the-scenes magic, since it can easily be taken too far, resulting in highly unreadable code -- not very Pythonic. - Tal Einat
participants (3)
-
Josiah Carlson
-
Pierre-Yves Martin
-
Tal Einat