<div dir="ltr"><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Zaur Shibzoukhov</b> <span dir="ltr"><<a href="mailto:szport@gmail.com">szport@gmail.com</a>></span><br>
Date: 2008/9/10<br>Subject: Re: [Python-ideas] should there be a difference between generators and iterators?<br>To: Bruce Frederiksen <<a href="mailto:dangyogi@gmail.com">dangyogi@gmail.com</a>><br><br><br>I think it should be as<br>

<br>
import types<br>
<br>
types.GeneratorType.__enter__ = MethodType(lambda self: self,<br>
types.GeneratorType)<br>
types.GeneratorType.__exit__ = MethodType(lambda self: self.close(),<br>
types.GeneratorType)<br>
<div class="Ih2E3d"><br>
>So that the objects returned by his generator functions (functions containing 'yield')<br>
>have context manager capability?<br>
</div>IMHO yes.<br>
<br>
Zaur<br>
<br>
2008/9/10 Bruce Frederiksen <<a href="mailto:dangyogi@gmail.com">dangyogi@gmail.com</a>>:<br>
<div><div></div><div class="Wj3C7c">> Zaur Shibzoukhov wrote:<br>
>><br>
>> Bruce Frederiksen wrote:<br>
>><br>
>>><br>
>>> Since there is no need to clean up after iterators in general, but only<br>
>>> for generators specifically; and since the BDFL has nixed my point #5,<br>
>>> it makes sense to only add the __enter__ and __exit__ to generators.<br>
>>> (And, by extension, itertools).<br>
>>><br>
>><br>
>> It makes more sense to allow extending of builtin types in order to<br>
>> support different protocols<br>
>> (not only protocol for "with" statement)<br>
>><br>
><br>
> Do you mean the python programmer should do:<br>
><br>
> import types<br>
><br>
> types.GeneratorType.__enter__ = lambda self: self<br>
> types.GeneratorType.__exit__ = lambda self: self.close()<br>
><br>
> So that the objects returned by his generator functions (functions<br>
> containing 'yield') have context manager capability?<br>
><br>
</div></div></div><br></div>