Python.with.context-manager

Source: Internet
Author: User

Context Manager1. Context Manager Introduction

"Context managers is a-allocating and releasing some sort of resource exactly where you need it." REF[4]

For example:

1 with file ("/tmp/foo""w") as foo:2      print"hello! "

Context Manager is introduced with the with.

"A context manager is an object that defines the runtime context to being established when executing A with  statemen T.

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 the context managers include saving and restoring various kinds of global state, locking and unlocking

Resources, closing opened files, etc. " REF[11]

2. Create a context Manager2.1Context 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 the separate methods, allow user-defined classes to define a runtime context, is entered before the Statemen T 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 being provided for a context manager

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

1 ContextManager. __enter__ ()23 contextmanager. __exit__ (Exc_type, Exc_val, EXC_TB)

Contextlib

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

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

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

undecorated generator function. " REF[6]

2.2 Using the Contextlib module

The simplest is using contextmanager the decorator contextlib from the library, and invoking yield in Y Our Context Manager

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

REF[4] in the example:

1 ImportContextlib2 Import Time3 4 @contextlib. ContextManager5 defTime_print (task_name):6t =time.time ()7     Try:8         yield9     finally:Ten         PrintTask_name,"took", Time.time ()-T,"seconds." One  AWith Time_print ("Processes"): -[Doproc () for_inchRange (500)] -  the #processes took 15.236166954 seconds. -  -With Time_print ("Threads"): -[Dothread () for_inchRange (500)] +  - #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/

Defining Context Managers The easy-to-do

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

One. With Statement Context Managers

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

Todo:

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

Python.with.context-manager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.