generators improvement

Oleg Leschov kalmas at udm.ru
Tue Aug 19 19:55:04 EDT 2003


Duncan Booth:
 DB> Oleg Leschov <kalmas at udm.ru> wrote in 
 DB> news:bhtiij$psc$2 at ocasysi.rubbernet.net:

>> What needs to be done is to allow yield return something - whatever was
>> passed to the .next() thing from caller..
>> Easy and obvious, isn't it? So is there any principal problem with this 
>> idea that would prevents its implementation?

 DB> Please read PEP 288, http://www.python.org/peps/pep-0288.html
 DB> in particular the section:

>> Rejected Alternative
>>     One idea for passing data into a generator was to pass an argument
>>     through next() and make a assignment using the yield keyword:
>> 
>>         datain = yield dataout
>>           . . .
>>         dataout = gen.next(datain)
>> 
>>     The intractable problem is that the argument to the first next() call
>>     has to be thrown away, because it doesn't correspond to a yield
>>     keyword.

not quite so... 
for example, such argument may be passed as the generator function's parameter, 
if it is defined.
Of cause, this would make first .next() and the rest of them distinct.
But, the question is - if there is .next(), why there's no .first()?
IMHO, current syntax does not truthfully reflect the nature of real generators.
These are not those that generate a sequence without the beginning, 
there is indeed a first element... While any but first .next() works with
concrete yield(), the first next() does not - it acts as a thing that starts
generator's execution from its entry point; thus, logically, it
should be different thing than the next() - perhaps .init() or .first() or .run(),
that either wouldn't take any arguments or will pass them as generator 
function's parameters (which are not allowed at all now, anyway).
Of cause, this would not cause any incompatibilities with current behavior,
as long as there is no arguments to .next() and no parameters for generator.





More information about the Python-list mailing list