I came across the Python with
statement for the first time today. I've been using Python lightly for several months and didn't even know of its existence! Given its somewhat obscure status, I thought it would be worth asking:
- What is the Python
with
statement designed to be used for? - What do you use it for?
- Are there any
gotchas I need to be aware of, or
common anti-patterns associated with
its use? Any cases where it is better use
try..finally
thanwith
? - Why isn't it used more widely?
- Which standard library classes are compatible with it?
with
in Python 3 documentation. – Internuncio