[Python-ideas] should there be a difference between generators and iterators?

Bruce Frederiksen dangyogi at gmail.com
Wed Sep 10 16:27:22 CEST 2008


Zaur Shibzoukhov wrote:
> Bruce Frederiksen wrote:
>   
>> Since there is no need to clean up after iterators in general, but only
>> for generators specifically; and since the BDFL has nixed my point #5,
>> it makes sense to only add the __enter__ and __exit__ to generators.
>> (And, by extension, itertools).
>>     
>
> It makes more sense to allow extending of builtin types in order to
> support different protocols
> (not only protocol for "with" statement)
>   
Do you mean the python programmer should do:

import types

types.GeneratorType.__enter__ = lambda self: self
types.GeneratorType.__exit__ = lambda self: self.close()

So that the objects returned by his generator functions (functions 
containing 'yield') have context manager capability?



More information about the Python-ideas mailing list