Are 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
Comments
Are you sure? I only see __exit__() being called after the loop: