[Python-ideas] Yield-from example: A parser

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Feb 18 04:07:09 CET 2009


Antoine Pitrou wrote:

>    def parse_items(closing_tag = None):
>      elems = []
>      while 1:
>        token = token_stream.next()
>        if not token:
>          break # EOF
>        [etc.]
> 
> It looks like parse_items pulls from token_stream until exhaustion.

Only the outermost call does that. The recursive calls
are made with a closing_tag value, and parse_items stops
as soon as it reaches that. Writing a for-loop would
give the erroneous impression that we were looping to
exhaustion, when most of the time we're not.

Also, even in the outer call, it stops when it gets a
token of None, which is half a loop sooner than the
scanner becomes exhausted. If a for-loop were used, it
would always be exited via a break.

Thirdly, if I had used a for-loop there, I would have
to have turned it into a while loop for the push version,
and that would have obscured the symmetry between the
two versions.

-- 
Greg



More information about the Python-ideas mailing list