[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Bruce Leban bruce at leapyear.org
Wed Oct 8 19:16:15 CEST 2008


Lisp's let: evaluate, evaluate, evaluate, assign, assign, assign
Lisp's let*: evaluate, assign, evaluate, assign, evaluate, assign

In Python as in Lisp, the side effects of the first evaluation are visible
to the second but in Python and Lisp's let (vs. let*) the assignment of the
first variable doesn't happen until after all the expressions have been
evaluated.

>>> def f(i=0, j=i+1):
    pass

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    def f(i=0, j=i+1):
NameError: name 'i' is not defined


On Wed, Oct 8, 2008 at 9:33 AM, Jim Jewett <jimjjewett at gmail.com> wrote:

> On Tue, Oct 7, 2008 at 9:57 PM, Bruce Leban <bruce at leapyear.org> wrote:
> > &aux is described here:
> > http://www.lispworks.com/documentation/HyperSpec/Body/03_dae.htm
> >
> > this says it's equivalent to let* which is described here:
> > http://www.lispworks.com/documentation/HyperSpec/Body/s_let_l.htm
> > In short &aux and let* evaluates each expression, assigns it to a
> variable
> > and then evaluates the next, etc. Default values in python are evaluated
> in
> > like Lisp's let, not let*.
>
>
> How do you figure?  As nearly as I can tell, the only difference is
> that let* is evaluated in order (left-to-right) instead of in
> parallel.
>
> Python parameters are also evaluated left-to-right, as nearly as I can
> tell.
>
>    >>> def f():
>        global var
>        var="from f"
>    >>> var="base"
>    >>> def h(): print "var is", var
>
>    >>> def g(a=f(), b=h()): print b
>
>    var is from f
>
> This shows that the side effect of binding a was already present when
> b was bound.
>
> -jJ
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081008/542c228e/attachment.html>


More information about the Python-ideas mailing list