Skip to content

Instantly share code, notes, and snippets.

@0x5742
Created September 13, 2015 21:12
Show Gist options
  • Select an option

  • Save 0x5742/1a53ef6963ad43f4cd19 to your computer and use it in GitHub Desktop.

Select an option

Save 0x5742/1a53ef6963ad43f4cd19 to your computer and use it in GitHub Desktop.
Don't do this. (It doesn't work anyway)
#!/usr/bin/python
import inspect
class let():
def __init__(self, **k):
self.k = k
def __enter__(self):
locs = self.framelocs = inspect.stack()[1][0].f_locals
self.oldlocs = locs.copy()
locs.update(self.k)
def __exit__(self, xt, xv, tb):
self.framelocs.clear()
self.framelocs.update(self.oldlocs)
def foo():
another="var"
with let(fizz=3, buzz=5):
print(locals())
print(fizz, buzz)
print(locals())
with let(fizz=3, buzz=5):
print(locals())
print(fizz, buzz)
#This fails as expected:
#print(fizz, buzz)
foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment