[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake
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
>>> def f(i=0, j=i+1):
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
> > and then evaluates the next, etc. Default values in python are evaluated
> > 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
> Python parameters are also evaluated left-to-right, as nearly as I can
> >>> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas