Is there "let binding" in Python?

R

Rob Hunter

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.

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.

As a simpler example, if I want to write:

(let ((x 3)) (+ x 4))

I can write

((lambda (x) (+ x 4)) 3) in Scheme

or,

(lambda x: x+4)(3) in Python.

I want to know if there's a less syntactically
cumbersome way of getting LET in python.

Thanks!

PS: I'm a newbie, so the answer in fact might be super-easy.

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
 
J

Jacek Generowicz

Rob Hunter said:
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". :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top