Is there "let binding" in Python?

Jacek Generowicz jacek.generowicz at cern.ch
Wed Sep 17 07:49:59 EDT 2003


Rob Hunter <rhunter007 at yahoo.com> writes:

> Is there an equivalent to Scheme's LET in Python?
>  LET creates a new binding in the current
> environment.  For example, here's some Scheme
> code:
> 
> (let ((x 3))
>   (let ((f (lambda (arg) (* arg x))))
>     (let ((x 4))
>       (f 5))))
> 
> So, this program, which also tests that Scheme
> has correct scoping, returns 15.

Sorry, never heard of "correct" scoping[*]. I've heard of lexical and
dynamic scoping though, and, to me, your code shows that Scheme has
lexical scoping.

> And in Python, I can write an equivalent program
> that does correctly return 15, but I have to use
> what, in Scheme, we call "left-left lambda"--a
> way of simulating LET.  Or rather, LET is
> syntactic sugar for left-left lambda.

A long, time ago (Python 2.0), Python only had 3 scopes: local, global
and builtin. Now it has nested scopes of arbitrary depth (still
wrapped inside global and builtin), but I'm not expecting "let" to
appear anytime soon.

[*] But given that you are formulating your question from a Scheme
    perspective, it's not really surprising that you call it
    "correct". :-)




More information about the Python-list mailing list