Python.with.context-manager

Context Manager

1. Context Manager简介

"Context managers are a way of allocating and releasing some sort of resource exactly where you need it."  Ref[4]

例如: 

1 with file("/tmp/foo", "w") as foo:
2     print >> foo, "Hello!"

Context Manager是随with一起引入的。

"A context manager is an object that defines the runtime context to be established when executing a with statement.

The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the

block of code."  Ref[11]

"Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking

resources, closing opened files, etc." Ref[11]

2. 创建Context Manager

2.1 Context Manager Type

with and context manager

"Python’s with statement supports the concept of a runtime context defined by a context manager. This is implemented

using two separate methods that allow user-defined classes to define a runtime context that is entered before the statement body

is executed and exited when the statement ends. " Ref[6]

context management protocol

"The context management protocol consists of a pair of methods that need to be provided for a context manager

object to define a runtime context:" Ref[6]

1 contextmanager.__enter__()
2 
3 contextmanager.__exit__(exc_type, exc_val, exc_tb)

contextlib

"Python’s generators and the contextlib.contextmanager decorator provide a convenient way to implement these protocols.

If a generator function is decorated with the contextlib.contextmanager decorator, it will return a context manager

implementing the necessary __enter__() and __exit__() methods, rather than the iterator produced by an

undecorated generator function." Ref[6]

2.2 使用contextlib模块

"the simplest way is using the contextmanager decorator from the contextlib library, and invoking yield in your context manager

function in between the setup and teardown steps." Ref[4]

Ref[4] 中的例子:

 1 import contextlib
 2 import time
 3 
 4 @contextlib.contextmanager
 5 def time_print(task_name):
 6     t = time.time()
 7     try:
 8         yield
 9     finally:
10         print task_name, "took", time.time() - t, "seconds."
11 
12 with time_print("processes"):
13     [doproc() for _ in range(500)]
14 
15 # processes took 15.236166954 seconds.
16 
17 with time_print("threads"):
18     [dothread() for _ in range(500)]
19 
20 # threads took 0.11357998848 seconds.

Reference

1. contextlib — Utilities for with-statement contexts

https://docs.python.org/2/library/contextlib.html

2. With Statement Context Managers

https://docs.python.org/2/reference/datamodel.html#context-managers

3. with statement

https://docs.python.org/2/reference/compound_stmts.html#with

4. Introduction to Context Managers in Python (Read Again)

http://eigenhombre.com/2013/04/20/introduction-to-context-managers/ 

5. Fixture functions using “yield” / context manager integration 

https://pytest.org/latest/yieldfixture.html

6. Build-in Types (To Read)

https://docs.python.org/2/library/stdtypes.html#typecontextmanager

7. PEP 343 -- The "with" Statement (To Read)

https://www.python.org/dev/peps/pep-0343/

8. Coding with context managers

http://python3porting.com/improving.html

9. Python: Tips, Tricks and Idioms - Part 2 - Decorators and Context Managers

https://codefisher.org/catch/blog/2015/02/10/python-decorators-and-context-managers/

10. Defining Context Managers the Easy Way

https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch09s22.html

11. With Statement Context Managers

https://docs.python.org/2/reference/datamodel.html#context-managers

Todo:

http://www.rafekettler.com/magicmethods.html

原文地址:https://www.cnblogs.com/cwgk/p/5096741.html