[Python-Dev] Withdrawn PEP 288 and thoughts on PEP 342

Guido van Rossum gvanrossum at gmail.com
Fri Jun 17 08:43:12 CEST 2005


[Raymond]
> Let me go on record as a strong -1 for "continue EXPR".  The for-loop is
> our most basic construct and is easily understood in its present form.
> The same can be said for "continue" and "break" which have the added
> advantage of a near zero learning curve for people migrating from other
> languages.
> 
> Any urge to complicate these basic statements should be seriously
> scrutinized and held to high standards of clarity, explainability,
> obviousness, usefulness, and necessity.  IMO, it fails most of those
> tests.
> 
> I would not look forward to explaining "continue EXPR" in the tutorial
> and think it would stand out as an anti-feature.

You sometimes seem to compound a rational argument with too much rhetoric.

The correct argument against "continue EXPR" is that there are no use
cases yet; if there were a good use case, the explanation would follow
easily.

The original use case (though not presented in PEP 340) was to serve
as the equivalent to "return EXPR" in a Ruby block. In Ruby you have
something like this (I probably get the syntax wrong):

  a.foreach() { |x| ...some code... }

This executes the block for each item in a, with x (a formal parameter
to the block) set to each consecutive item. In Python we would write
it like this of course:

  for x in a:
      ...some code...

In Ruby, the block is an anonymous procedure (a thunk) and foreach() a
method that receives a margic (anonymous) parameter which is the
thunk. Inside foreach(), you write "yield EXPR" which calls the block
with x set to EXPR. When the block contains a return statement, the
return value is delivered to the foreach() method as the return value
of yield, which can be assigned like this:

  VAR = yield EXPR

Note that Ruby's yield is just a magic call syntax that calls the thunk!

But this means that the thunks can be used for other purposes as well.
One common use is to have the block act as a Boolean function that
selects items from a list; this way you could write filter() with an
inline selection, for example (making this up):

  a1 = a.filter() { |x| return x > 0 }

might set a1 to the list of a's elements that are > 0. (Not saying
that this is a built-in array method in Ruby, but I think you could
write one.)

This particular example doesn't translate well into Python because a
for-loop doesn't have a return value. Maybe that would be a future
possibility if yield-expressions become accepted (just kidding:-).

However, I can see other uses for looping over a sequence using a
generator and telling the generator something interesting about each
of the sequence's items, e.g. whether they are green, or should be
printed, or which dollar value they represent if any (to make up a
non-Boolean example).

Anyway, "continue EXPR" was born as I was thinking of a way to do this
kind of thing in Python, since I didn't want to give up return as a
way of breaking out of a loop (or several!) and returning from a
function.

But I'm the first to admit that the use case is still very much
hypothetical -- unlike that for g.next(EXPR) and VAR = yield.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list