PEP 255: Simple Generators

Andrew Dalke dalke at acm.org
Sat Jun 23 07:09:53 EDT 2001


Carsten Geckler:
>Could you tell me, how e.g. a "generator"  statement would
>be distracting?  I can't see how a more clearifying statement could
>possibly distract people.

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.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list