PEP 255: Simple Generators

Carsten Geckeler uioziaremwpl at spammotel.com
Sat Jun 23 10:23:48 EDT 2001


On Sat, 23 Jun 2001, Andrew Dalke wrote:

> Me: Okay, the "def" statement is how you make a function.
> Chemist: Yeah, functions, I know those from FORTRAN.
> Me: And the "class" statement is how to make a new class.
> Chemist: Ohh! I finally get to learn about object-oriented programming!
> Me: And the "generator" statement is how to make a new generator.
> Chemist: A what?  What's a generator?
> Me: It's a function that returns a special kind of object.
> Chemist: Then why isn't it called a function?
> Me: Well, you can think of a class as a function that returns
>   a special kind of object.
> Chemist: Oooo..kaaay..
> Me: Okay, how about this.  A generator turns a function into ...
>   You know, I just don't know how to explain it well enough.
>   But they are really easy to use - just pretend they are functions
>   that return a list, except that you can only ask for the next
>   item in the list.
> Chemist: But you said Python was also easy to understand.

Since we now have "def"/"yield" for generators, the discussion goes more
or less the same way.  You just realizes that generators seem to be to
complicated for first time Python users.  So you decide not to mention
them at the beginning, but later with the other advanced features.

So some days later, another friend comes to you.  You explain him all the
basic programming features, but not generators.  A week later, the friend
comes to you again.

Friend:	Well, I programmed a little bit with Python.  It really seems to
	be very simple.  I have just one problem: I've found a code
	example somewhere on the web and there is a function in it, which
	seems to behave a little bit strange.  Perhaps you can help me.
Me:	OK, let me see.  Oh, right, this is a generator, not a function!
Friend:	A what?  It seems to be like a function but it has some yield
	statements in it.  What does yield do?
Me:	Well, it returns an object and suspends the evaluation of the
	block till the next iteration.  Generators are special functions.
Friend:	So yield does the same as return?  Actually it has to be, because
	there is no return in the, ehh, generator, but it returns some
	object.  So the returned value comes from the yield statement.
Me:	No.  I'll have to explain generators in more detail.  When the
	generator is called, it returns an iterator without evaluating
	any of the code in the definition.
Friend:	Returns an iterator?  But when is the code executed then, if not
	while calling it?
Me:	It returns an iterator object.  Then each time you call
	iterator.next(), the code is executed until it reaches the next
	yield, and the value after yield is return.
Friend:	So it's not a function?!?  Because you told me last time that
	functions just execute the given statements in order.
Me:	Yes, it's not a function, it's a generator.  If you have a def
	statement with a yield statement, it's a generator.
Friend:	That's confusing.  So def has two meanings depending on an
	occurence of yield or not?

And so on....

Cheers, Carsten
-- 
Carsten Geckeler






More information about the Python-list mailing list