PEP 255: Simple Generators
tim.one at home.com
Wed Jun 20 01:57:28 CEST 2001
> def f():
> 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'
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
> Or consider a case like this:
> def f(x):
> if x == 0:
> elif x == 1:
> # ...
> 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