Skip to content

Instantly share code, notes, and snippets.

@ijp
Created March 29, 2015 14:55
Show Gist options
  • Save ijp/2b86da261569a314b9ff to your computer and use it in GitHub Desktop.
Save ijp/2b86da261569a314b9ff to your computer and use it in GitHub Desktop.
# a sketch of how Scheme fluids/parameters would work in python
obarray = {}
class Manager(object):
def __init__(self, parameter, value):
self.parameter = parameter
self.value = value
def __enter__(self):
self.old_value = self.parameter.get()
obarray[self.parameter] = self.value
def __exit__(self, exc_type, exc_value, traceback):
## A real pro would handle exceptions
obarray[self.parameter] = self.old_value
class Parameter(object):
def __init__(self, value):
obarray[self] = value
def get(self):
return obarray[self]
def __call__(self, new_value):
return Manager(self, new_value)
current_foo = Parameter(10)
print "1", current_foo.get()
with current_foo(20):
print "2", current_foo.get()
with current_foo(30):
print "3", current_foo.get()
print "2", current_foo.get()
print "1", current_foo.get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment