RELEASED Python 2.4, alpha 1

Michele Simionato michele.simionato at gmail.com
Sat Jul 10 02:43:11 EDT 2004


Tim Peters <tim.peters at gmail.com> wrote in message news:<mailman.186.1089406569.5135.python-list at python.org>...
> Guido Pronounced:  the expression in the leftmost "for" clause is
> evaluated immediately, but all the rest is delayed.  So in your
> example, only "[1, 2, 3]" is evaluated at the time the genexp is
> created.  If you had tried to iterate instead over, say, range(1/0),
> the ZeroDivisionError would have been raised immediately, which is the
> real point of evaluating that one piece "early".
> 
> Don't ask me to justify the rest <wink>.
> 
> Guido doesn't really care about examples like yours.  He thinks
> genexps will overwhelmingly be consumed "on the same line" they're
> created, and that people doing fancy-pants stuff like you're doing
> there probably shouldn't (but could find more-or-less obvious
> workarounds if they had to, given that they're obsessed enough to try
> such fancy-pants stuff to begin with).
> 
> The world won't end either way (IMO), and (also IMO) Python has pushed
> delayed code blocks in a scoped language without explicit scope
> declarations about as far as it can without becoming plainly
> incomprehensible.

It happened to me more than once to think that Guido made something 
wrong and to change my mind months later. So this maybe one of those
occasions. The problems is mostly for people coming from functional
languages. Incidentally, I got caught by this a couple of year ago
and this was the reason for my first post on the newsgroup (I was
playing with Tkinter at the time and lambda's are useful as callback
functions). At the time I had no experience with functional languages,
but still I had a functional mindset due to my strong mathematical 
background and experience with Mathematica/Maple.

The simplest workaround is Pythonic in the sense that it is explicit
 
f1,f2,f3=tuple(lambda i=i: i for i in [1,2,3])

as one explicitly rebinds "i" at each iteration but still I cannot
find it other than hackish, since there is no point here in creating
a function with default arguments other than fixing the
binding-in-iteration
issue. What I would need is a better way to create function objects
than
lambda's; for instance I would like the ability to subclass the
function type and customize it to my needs (but I have already talked
about this in the
past
http://groups.google.it/groups?hl=it&lr=&ie=UTF-8&selm=95aa1afa.0402262158.5b33de79%40posting.google.com&rnum=1
so I want repeat myself).
If I had that, the binding-in-iteration issue would be minor for me
and I
would not protest anymore. Actually I would probably think that it is
good
to have a broken binding-in-iteration behavior, so people are
encouraged
not to use lambda's and to generate their functions in other ways. 

                   Michele Simionato



More information about the Python-list mailing list