Back to index

python3.2  3.2.2
Classes | Functions | Variables
contextlib Namespace Reference

Classes

class  ContextDecorator
class  _GeneratorContextManager
class  closing

Functions

def contextmanager

Variables

list __all__ = ["contextmanager", "closing", "ContextDecorator"]

Detailed Description

Utilities for with-statement contexts.  See PEP 343.

Function Documentation

def contextlib.contextmanager (   func)
@contextmanager decorator.

Typical usage:

    @contextmanager
    def some_generator(<arguments>):
        <setup>
        try:
            yield <value>
        finally:
            <cleanup>

This makes this:

    with some_generator(<arguments>) as <variable>:
        <body>

equivalent to this:

    <setup>
    try:
        <variable> = <value>
        <body>
    finally:
        <cleanup>

Definition at line 85 of file contextlib.py.

00085 
00086 def contextmanager(func):
00087     """@contextmanager decorator.
00088 
00089     Typical usage:
00090 
00091         @contextmanager
00092         def some_generator(<arguments>):
00093             <setup>
00094             try:
00095                 yield <value>
00096             finally:
00097                 <cleanup>
00098 
00099     This makes this:
00100 
00101         with some_generator(<arguments>) as <variable>:
00102             <body>
00103 
00104     equivalent to this:
00105 
00106         <setup>
00107         try:
00108             <variable> = <value>
00109             <body>
00110         finally:
00111             <cleanup>
00112 
00113     """
00114     @wraps(func)
00115     def helper(*args, **kwds):
00116         return _GeneratorContextManager(func, *args, **kwds)
00117     return helper
00118 

Here is the call graph for this function:


Variable Documentation

list contextlib.__all__ = ["contextmanager", "closing", "ContextDecorator"]

Definition at line 7 of file contextlib.py.