with statement and context managers

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Aug 5 02:40:08 CEST 2011


Thomas Rachel wrote:

> Am 03.08.2011 04:15 schrieb Steven D'Aprano:
[...]
>  > but to me that looks badly wrong. Surely the spam context manager
>  > object will exit after the first iteration, and always raise an
>  > exception on the second? But I don't quite understand context
>  > managers enough to be sure.
> 
> Depends on the implementation. As already stated, a
> contextlib.contextmanager will only run once. But it is easy to turn it
> into a persistent one - which internally initializes as often as needed.

Thanks, that's exactly the sort of information I was after.

Thank you to everyone who answered.


-- 
Steven




More information about the Python-list mailing list