[Python-Dev] More Switch: Explicit freezing (original) (raw)
Talin talin at acm.org
Sat Jul 1 22:32:02 CEST 2006
- Previous message: [Python-Dev] doc for new restricted execution design for Python
- Next message: [Python-Dev] More Switch: Explicit freezing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Here's another stab at the "explicit freezing" school of thought on the switch semantics. The idea is to borrow the freeze protocol and apply it to functions.
In this scheme, the default behavior of switch is to rebuild the dictionary each time the switch is executed. However, by calling freeze(), you can get a 'frozen' version of the function in which all switch dictionaries contained within the function are precalculated:
# Emulating 'freeze at function definition time'
def myfunc( x ):
switch y:
case a:
...
case b:
...
myfunc = freeze( myfunc )
This of course lends itself well to decorator syntax:
@freeze
def myfunc( x ):
switch y:
case a:
...
case b:
...
You can also get 'freeze on first use' via the appropriate decorator function, although that's a litte harder to white (essentially, you need a way to test if the function is already frozen.)
Each time you call freeze(), you get a new copy of the function object with the switch dictionaries bound to the values that were in the scope of the call to 'freeze'. This means that you can call freeze several times and get several different versions of the function:
def myfunc( x ):
switch y:
case a:
...
case b:
...
a = 1
b = 2
f1 = freeze( myfunc )
a = 3
b = 4
f2 = freeze( myfunc )
Now we have two versions of the function, each having a different switch dictionary.
Note that 'switch' is still usable without 'freeze', it just won't run as fast. This means that the folks who are interested in a switch statement purely for its improved expressiveness can simply not bother with freezing the function.
-- Talin
- Previous message: [Python-Dev] doc for new restricted execution design for Python
- Next message: [Python-Dev] More Switch: Explicit freezing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]