3 ms·
The problem I have with async with is that you've already got issues with the syntax for "with" context managers and generators. def my_generator():
by cbsmith 11y ago
The problem I have with async with is that you've already got issues with the syntax for "with" context managers and generators.
def my_generator():
with something() as bar:
for x in baz():
yield bar(x) # context manager will call bar.__exit__() before the second time through this loop.
It'd make a LOT more sense to just make "with" understand when it is in a continuation and only invoke __exit__() when the continuation is being destroyed.
- laurencerowe 11y agoAre you sure? I only see __exit__() being called after the loop: Python 3.4.2 (default, Dec 23 2014, 17:01:26) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from contextlib import contextmanager >>> @contextmanager ... def printme(): ... print('enter') ... yield ... print('exit') ... >>> def my_generator(): ... with printme(): ... for x in range(3): ... yield x ... >>> for x in my_generator(): ... print(x) ... enter 0 1 2 exit
- rat87 11y agoI think that's wrong, def lazy_lines(filename): with open(filename) as f: for line in f: yield f works just fine