[Python-Dev] PEP Idea: Syntactic sugar for StopIteration.
Steve Holden
steve at holdenweb.com
Sat Dec 8 23:04:05 CET 2007
Manuel Alejandro Cerón Estrada wrote:
> 2007/12/8, Raymond Hettinger <python at rcn.com>:
>> ...the proposal adds new syntax without adding functionality.
>
> That is indeed the definition of syntactic sugar [1]. Python is full
> of that, for example the import statement.
>
> 2007/12/8, Paul Svensson <paul at svensson.org>:
>> What is the problem that is not solved by iter(()) ?
>
> 'yield iter(()).next()' it's obscure, you can't know its purpose at
> first sight. There are other alternatives but none of them seems to be
> the "obvious way to do it".
>
But surely iter(()) itself (and indeed iter(any empty sequence) is
precisely the empty generator you seek, without having to use a def
statement to create your own in Python.
>>> for i in iter(()):
... print "Result"
...
>>>
So I don't see what's so magical about creating your own empty iterator,
nor why you consider it necessary. The 2002 discussion to which you
refer is clearly outdated.
> But that is the less important problem. The real problem is that
> raising StopIteration is not orthogonal. It is confusing for the
> reasons I exposed. Generators are something in the middle between a
> language feature and a framework/library. With 'yield break' they will
> be completely a language feature.
>
> [1] http://en.wikipedia.org/wiki/Syntactic_sugar
>
It's necessary, in discussions of syntactic sugar, to admit from the
start that a certain amount of sweetness is desirable. If you would
really like to remove the import statement you might consider
programming in brainfuck, one of the least sugary languages around.
http://en.wikipedia.org/wiki/Brainfuck
Of course, Python is popular because it hits many people's "sweet spot"
for the desirable balance between syntactic sugar and minimalism, making
it possible to write compact solutions to advanced problems. There are
definitely limits, though. "yield break" seems ugly and unnecessary to me.
I'd rather consider Icon's "break break" to exit two levels of looping,
but I don't see that flying either.
You still haven't made a convincing use case for "yield break", IMHO.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-Dev
mailing list