Is there "let binding" in Python?

Rob Hunter rhunter007 at yahoo.com
Tue Sep 16 06:20:08 CEST 2003


> It's not mutation, it's assignment.
> 

I had always used the word mutation basically for
any use of a "!" in Scheme, but fair enough.

> Mutation means changing an object in some way
> (i.e., you have the same
> identical object after as before); assignment
> is just associating a
> different object with a variable.
> 
> > In scheme, you could write something like
> this:
> 
> > (let loop ((i 1))
> >   (if (> i 10)
> >       empty
> >       (cons (make-button i)
> >             (loop (+ 1 i)))))
> 
> > This program returns a list of 10 buttons
> > (assuming "make-button" creates numbered
> buttons)
> > from 1 to 10.
> 
> > IMO, this looks a lot like your button code,
> > except there's no mutation and thus no
> problem of
> > making every button be button 10.
> 
> There's no assignment, because it's a recursive
> procedure in Scheme,
> which ensures a new binding for i each time
> through the loop.
> 

Well, yeah.  And that's a good thing, right?  I
mean, perhaps not in general, but certainly in
this case I think everyone can agree that it is
*assignment* which creates the confusing issue of
all buttons being named 10.


<snipping>



> 
> No, you don't introduce a binding with "="
> 
> Python people will tell you otherwise, but
> that's a result of failing
> to understand the difference between binding
> and assignment -- they
> think "binding" means what everybody else in
> the world calls
> "assignment", and they don't seem to recognise
> that the thing the rest
> of the world calls "binding" is a different
> thing.
> 
> In Python, the compiler examines the code (at
> compile time, naturally)
> and notices any assignments; it then inserts a
> binding for any
> variables that are assigned in a particular
> block of code at the start
> of that block.  I.e., writing something like
> 
>    def foo():
>        blah()
>        x = 7
>        blah()
> 
> compiles as if you'd written
> 
>    (define foo
>      (lambda ()
>        (let ((x ..magic-unbound-value..))   ;
> here's the binding
>          (blah)
>          (set! x 7)                         ;
> this is assignment
>          (blah))))
> 
> in Scheme.  The binding (let) is inserted by
> the compiler and doesn't
> occur at the same place as the assignment -- it
> really has nothing to
> do with the "=" at all.
> 
> That's why you can't write
> 
>    def foo():
>       print x
>       x = 42
>       print x
> 
> and expect the first print statement to print
> the global value of x
> (assuming there is one) -- because the later
> assignment causes a
> binding to be inserted *before* the earlier
> print, so it tries to
> print the value of the not-yet-assigned local x
> (and the initial value
> is the "magic unbound value", which causes an
> error if you try to look
> at it)

I believe you when you say this is how Python
does it, but I'd like to know what's wrong with
the model I have.  Can you give me an example
where my model fails me?  To review, I am saying,
now just for the sake of argument and
understanding Python, that the first "=" is
special and acts like a Scheme LET while
subsequent "=" with the same variable name in
scope act like assignments.

So your Python code:

def foo():
       print x
       x = 42
       print x

would be in *your* world (in scheme):

(define (foo)
  (let ((x --special unbound thingy--))
    (print x)
    (set! x 42)
    (print x)))

and in my world would be:

(define (foo)
  (print x)
  (let ((x 42))
    (print x)))

To be clear, what I am assuming is that Python is
our "proposed" language, and Scheme is our
semantics.  So I'm giving a single Python program
two different "meanings."

*Except*, there's nothing different about these
Scheme programs, really.  I'm being a little
informal, obviously, but both programs error on
the first (print x) and both, if the first (print
x) were removed, would succeed in exactly the
same way.

So, in summary, you seem to be stressing the
importance of the fact that Python does not
introduce a binding with (the first) "=".  But I
don't see why.

Thanks,
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