Other builtin or practical examples of python `with` statement usage?

给你一囗甜甜゛ 提交于 2019-12-05 15:30:28

There are many uses. Just in the standard library we have:

  • sqlite3; using the connection as a context manager translates to committing or aborting the transaction.

  • unittest; using assertRaises as a context manager lets you assert an exception is raised, then test aspects of the exception.

  • decimal; localcontext manages decimal number precision, rounding, and other aspects.

  • threading objects such as locks, semaphores and conditions are context managers too; letting you obtain a lock for a set of statements, etc.

  • the warnings module gives you a context manager to temporarily catch warnings.

  • many libraries offer closing behaviour, just like the default file object. These include the tarfile and the zipfile modules.

  • Python's own test.test_support module uses several context managers, to check for specific warnings, capture stdout, ignore specific exceptions and temporarily set environment variables.

Any time you want to detect when a block of code starts and / or ends, you want to use a context manager. Where before you'd use try: with a finally: suite to guarantee a cleanup, use a context manager instead.

Christian Ternus

There are a few examples on the Python Wiki.

The canonical answer is locks:

with (acquire some mutex):
    # do stuff with mutex

Here's a Stack Overflow question and answer involving locks and the with statement.

I've found it useful to have a contextmanager version of os.chdir(): on exit it chdir()s back to the original directory.

This allows you to emulate a common (Bourne) shell-scripting pattern:

(
cd <some dir>
<do stuff>
)

I.e. you change to a new dir <some dir> inside a subshell (the ( )) so that you are sure to return to your original dir, even if the <do stuff> causes an error.

Compare the context manager and vanilla versions in Python. Vanilla:

original_dir = os.getcwd()
os.chdir(<some dir>)
try:
    <do stuff>
finally:
    os.chdir(original_dir)

Using a context manager:

with os.chdir(<some dir>):
    <do stuff>

The latter is much nicer!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!