My intended behavior is:
>>> x = 0
>>> with (x := 1): print(x)
1
>>> print(x)
0
However I get the expected AttributeError: __enter__
error. Is there an easy way to achieve this, or something similar that lets me compensate for not having Lisp-style let expression?
P.S. I know I can do something like:
class Let(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __enter__(self):
return self
def __exit__(self, *args):
return None
And then:
>>> with Let(x=1) as let: print(let.x)
Or:
>>> with (let := Let(x=1)): print(let.x)
But having to say let.x
as opposed to just x
makes it too ugly!
:=
operation isn't likelet
in JavaScript, it's a operation to assign a value to a variable and output the value at the same time. Loosely speaking,let
is declare a local variable ie. you can't use this variable outside the scope.I'm not sure what you mean by 'let expression', but
:=
is different from scope differentiated assignment.