[Python-Dev] PEP 340 -- loose ends

Shane Holloway (IEEE) shane.holloway at ieee.org
Thu May 5 01:29:39 CEST 2005


Shane Hathaway wrote:
> For each block statement, it is necessary to create a *new* iterator,
> since iterators that have stopped are required to stay stopped.  So at a
> minimum, used-defined statements will need to call something, and thus
> will have parentheses.  The parentheses might be enough to make block
> statements not look like built-in keywords.

Definitely true for generators.  Not necessarily true for iterators in 
general::

     class Example(object):
         value = 0
         result = False

         def __iter__(self):
             return self

         def next(self):
             self.result = not self.result
             if self.result:
                 self.value += 1
                 return self.value
             else:
                 raise StopIteration()

::

     >>> e = Example()
     >>> list(e)
     [1]
     >>> list(e)
     [2]
     >>> list(e)
     [3]


It might actually be workable in the transaction scenario, as well as 
others.  I'm not sure if I love or hate the idea though.

Another thing.  In the specification of the Anonymous Block function, is 
there a reason that "itr = EXPR1" instead of "itr = iter(EXPR1)"?  It 
seems to be a dis-symmetry with the 'for' loop specification.

Thanks,
-Shane (Holloway)  ;)


More information about the Python-Dev mailing list