PEP 255: Simple Generators

Tim Peters tim.one at home.com
Wed Jun 20 01:42:08 CEST 2001


[Carsten Geckeler]
> ...
> But when I'm browsing through a source it may help me very much
> if I could see at the `def' line if it's a regular function or a
> generator.

I simply don't believe it.  I used generators very happily for years in
Icon, which also makes no syntactic distinction, and it never occurred to me
it might help to make one.  There's almost nothing interesting about a
Python function you can determine without good docs, and it's not PEP 255's
burden to repair that for those who feel it's a problem.  With optional
static typing, you could say that a function returned a generator-iterator,
and that would also cover functions that *do* return a generator-iterator
but are not *themselves* generator-functions.  That is, the ability to
return generator-iterators is not limited to generator-functions, so the
idea that a "gen" keyword would actually help is straining credibility on
that point too.

> ...
> I want to catch it.  So why not raise it explicitly as "raise StopIter"
> when I have to catch it explicitly as "except StopIter" anyway?

You have unusual uses in mind, then -- I haven't seen an example, either
real or proposed, of actual generator use that wants to know about the
exception at all.  You can raise an exception explictly if you want to, but
most people won't even want to know about it -- exactly as *most* Python
programmers today don't know that a "for" loop in 2.1 (and before) is
actually terminated by a suppressed IndexError exception.





More information about the Python-list mailing list