[Python-Dev] PEP 340 -- loose ends

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue May 3 06:59:42 CEST 2005


Guido van Rossum wrote:

> Oops. Read the most recent version of the PEP again. __next__()
> doesn't take an exception argument, it only takes a value. Maybe this
> removes your concern?

Actually, I misinterpreted it, assuming that the value passed in was an
exception instance because the previous versions worked that way. This
has been going on too long ;)

> Ah, here we see the other misconception (caused by not reading the
> most recent version of the PEP). __exit__() shouldn't call __next__()
> -- it should just raise the exception passed in unless it has
> something special to do.

Ah - I think this needs to be explained better. In particular, in the
specification of the __next__ and __exit__ methods it should state what
exceptions are expected to be raised under what circumstances - in
particular, that __exit__ is expected to raise the passed in exception
or StopIteration. This is only explained in the Generator Exception
Handling specification, but it's applicable to all iterators.

>> Finally, I think there is another loose end that hasn't been
>> addressed:: 
>> 
>>     When __next__() is called with an argument that is not None, the
>>     yield-expression that it resumes will return the value attribute
>>     of the argument.  If it resumes a yield-statement, the value is
>>     ignored (or should this be considered an error?).  When the
>>     *initial* call to __next__() receives an argument that is not
>>     None, the generator's execution is started normally; the
>>     argument's value attribute is ignored (or should this be
>>     considered an error?).  When __next__() is called without an
>>     argument or with None as argument, and a yield-expression is
>>     resumed, the yield-expression returns None.
> 
> Good catch.
> 
>> My opinion is that each of these should be an error.
> 
> Personally, I think not using the value passed into __next__() should
> not be an error; that's about the same as not using the value returned
> by a function you call.

Now that I understand that the parameter to __next__ is not an
exception, I agree.

> I agree that calling the initial __next__() of a generator with a
> non-None argument should be considered an error; this is likely caused
> by some kind of logic error; it can never happen when the generator is
> called by a block statement.

Cheers.

Tim Delaney


More information about the Python-Dev mailing list