Skip to content

Instantly share code, notes, and snippets.

@kachayev
Created April 3, 2013 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachayev/5302369 to your computer and use it in GitHub Desktop.
Save kachayev/5302369 to your computer and use it in GitHub Desktop.
Can you guess how it's possible? (yes, this is python)
>>> (let (a=2, b=4) (a + b))
6
>>> (let (x=20, y=40, z=1) (x*y*z))
800
@alexsv
Copy link

alexsv commented Apr 3, 2013

def let(**kwargs):
..globals().update(kwargs)
..def f(v):
....return v
..return f

@nhoad
Copy link

nhoad commented Apr 3, 2013

import sys

class let(object):
    def __init__(self, **kwargs):
        self.f_locals = f_locals = sys._getframe().f_back.f_locals
        self.orig_locals = dict(f_locals)
        f_locals.update(kwargs)

    def __call__(self, expr):
        return expr

    def __del__(self):
        self.f_locals.clear()
        self.f_locals.update(self.orig_locals)

assert (let (a=2, b=4) (a+b) ) == 6
assert (let (x=20, y=40, z=1) (x*y*z)) == 800

@kachayev
Copy link
Author

kachayev commented Apr 3, 2013

@nathan-hoad Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment