Prothon gets Major Facelift in Vers 0.1.0 [Prothon]

Mark Hahn mark at prothon.org
Mon May 24 23:55:53 EDT 2004


Josiah Carlson wrote:

> You use inconsistant descriptions of generators here:
> http://prothon.org/tutorial/tutorial11.htm#gen
>
> First you say that all generators must use 'gen' rather than 'def',
> but
> in your example you mix the two...
>
> gen evenOdds(max):
>      def evens(max):
>          (body contains a yield)
>
> Based on that same portion of your tutorial, it is not clear that your
> example...
>
> gen evenOdds(max):
>      def evens(max):
>          num = 0
>          while num < max:
>              yield num
>              num += 2
>
>      def odds(max):
>          num = 1
>          while num < max:
>              yield num
>              num += 2
>
>      evens(max)
>      odds(max)
>
> for i in evenOdds(10):
>      print i,   #  prints 0 2 4 6 8 1 3 5 7 9
>
> Actually should produce what you say it should.

The code is correct and tested.  My programming skills are much better than
my tutorial writing skills :-)

> Perhaps it is my background in Python that says unless you manually
> iterate through evens(max) and odds(max), yielding values as you go
>      along... for i in evens(max): yield i
>      for i in odds(max): yield i
> ...or combine and return the iterator with something like...
>      return itertools.chain(evens(max), odds(max))
> ..., you will get nothing when that generator function is evaluated.
>
> I believe that either your documentation or example needs to be fixed.

I'm sure that my tutorial could be clearer, but in my defense I do say in
that section:  "only the one outermost "function" should use the "gen"
keyword".

The way it works is that the "gen" keyword is just a special flag to tell
the interpreter to stop rolling up the execution frame stack when a yield
keyword is encountered.  This is what allows the functions to be nested. The
yield keyword can be in any function whether it uses the gen keyword or the
def keyword.

I do think you are confusing it with Python, which cannot nest functions
with yield statements as Prothon can.





More information about the Python-list mailing list