yield

John J. Lee jjl at pobox.com
Sun Jan 18 07:27:57 EST 2004


Isaac To <kkto at csis.hku.hk> writes:

> >>>>> "km" == km  <km at mrna.tn.nic.in> writes:
> 
>     km> Hi all, i didnt understand the purpose of 'yield' keyword and the
>     km> concept of 'generators' in python.  can someone explain me with a
>     km> small example how generators differ from normal function calls?
> 
> Normally, when you define a function and call it, the code within the
> function gets executed, until the function returns, and at that point the
> function disappear altogether.  For example:
[...]

That's a misleading way of putting it: the *function* doesn't
disappear (you can still call it after it's finished), but its state
of execution does (you can't access its local variables after it's
finished, except by calling it again and getting a *new* set of local
variables).


John



More information about the Python-list mailing list