Is there "let binding" in Python?

Rob Hunter rhunter007 at yahoo.com
Tue Sep 16 22:31:39 CEST 2003


> Compare:
> 
> >>> def foo():
>    ...    print x
>    ...    # x = 42  # commented out
>    ...    print x
>    ...
> >>> x = 7
> >>> foo()
>    7
>    7
> >>> x = 10
> >>> foo()
>    10
>    10
> 
> Versus:
> 
> >>> def foo():
>    ...    print x
>    ...    x = 42
>    ...    print x
>    ...
> >>> x = 7
> >>> foo()
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in ?
>      File "<stdin>", line 2, in foo
>    UnboundLocalError: local variable 'x'
> referenced before assignment
> 
> 
> Your
> 
>    (define (foo)
>      (print x)
>      (let ((x 42))
>        (print x)))
> 
> interpretation can't explain that result -- if
> the LET comes *after*
> the first print, the first print would print
> the value in the original
> (global) binding, and you'd see
> 
> >>> foo()
>    7
>    42
> 
> rather than an error.
> 
> 


I'm quite confused now. :)  Actually, I
understand (I think) the examples you gave me,
but I'm left with a bad taste in my mouth: you
know, that "assignment is really nasty" taste.

Rob

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





More information about the Python-list mailing list