PEP 255: Simple Generators

Tim Peters tim.one at home.com
Tue Jun 19 19:57:28 EDT 2001


[Rainer Deyke]
> ...
> Consider:
>
> def f():
>   return
>   yield 'Never reached.'
>
>
> Is this function a generator?

Yes, but a pretty useless one.  Python's not going to prevent you from
writing useless code, else 99% of programming jobs would vanish <wink>.

> Yes, but only if nobody removes the seemingly redundant 'yield'
> statement.

If they think it's redundant, they don't understand the language well enough
that sticking "gen" on it instead of "def" is magically going to give them
an education.

> Or consider a case like this:
>
> def f(x):
>   if x == 0:
>     do_something()
>   elif x == 1:
>     do_something_else()
>   # ...
>   elif x == 99:
>     yield 5

Why?  That is, it's obviously contrived.  Play with the feature and find a
problem that actually bites you.  I'm not concerned if a determined newbie
with bad programming habits ("Here we have a very long function that doesn't
fit on the screen all at once") can get themself confused.

it's-called-"simple-generators"-not-"idiot-proof"-ly y'rs  - tim





More information about the Python-list mailing list