April 10 2014 Meeting Notes
Jason Orendorff
jason.orendorff at gmail.com
Thu Apr 24 13:24:59 PDT 2014
On Thu, Apr 24, 2014 at 2:16 PM, Brendan Eich <brendan at mozilla.org> wrote:
> The only proposal (let's table whether the name is 'return' or @@return) is
> whether for-of constructs should have a finally maybe-call of the iterator's
> return method, if present. Like SpiderMonkey's JS1.7-1.8 close method, based
> in part on Python 2.5 and up.
But in Python, closing iterators is part of the protocol. It isn't
mentioned in the language specification (Python does not have a
detailed spec) but all language and library facilities that consume
iterables do it, not just for loops.
For example, given this generator:
def f():
try:
yield 0
finally:
print("closing")
note the lines that say "closing" in both examples below:
>>> [FAIL for i in f()]
closing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'FAIL' is not defined
>>> map(lambda: FAIL, f())
closing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes no arguments (1 given)
Even things like zip(), which can't be written in terms of a for loop,
close iterators on error.
-j
More information about the es-discuss
mailing list