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

Zaur Shibzoukhov szport at gmail.com
Wed Sep 10 20:49:08 CEST 2008


---------- Forwarded message ----------
From: Zaur Shibzoukhov <szport at gmail.com>
Date: 2008/9/10
Subject: Re: [Python-ideas] should there be a difference between generators
and iterators?
To: Bruce Frederiksen <dangyogi at gmail.com>


I think it should be as

import types

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

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

Zaur

2008/9/10 Bruce Frederiksen <dangyogi at gmail.com>:
> 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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080910/cfb42ee1/attachment.html>


More information about the Python-ideas mailing list