<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>27.6. contextlib — Utilities for with-statement contexts — Python v2.6.6 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="top" title="Python v2.6.6 documentation" href="../index.html" />
<link rel="up" title="27. Python Runtime Services" href="python.html" />
<link rel="next" title="27.7. abc — Abstract Base Classes" href="abc.html" />
<link rel="prev" title="27.5. warnings — Warning control" href="warnings.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="abc.html" title="27.7. abc — Abstract Base Classes"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="warnings.html" title="27.5. warnings — Warning control"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="python.html" accesskey="U">27. Python Runtime Services</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-contextlib">
<h1>27.6. <tt class="xref docutils literal"><span class="pre">contextlib</span></tt> — Utilities for <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a>-statement contexts<a class="headerlink" href="#module-contextlib" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.5.</span></p>
<p>This module provides utilities for common tasks involving the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a>
statement. For more information see also <a class="reference external" href="stdtypes.html#typecontextmanager"><em>Context Manager Types</em></a> and
<a class="reference external" href="../reference/datamodel.html#context-managers"><em>With Statement Context Managers</em></a>.</p>
<p>Functions provided:</p>
<dl class="function">
<dt id="contextlib.contextmanager">
<tt class="descclassname">contextlib.</tt><tt class="descname">contextmanager</tt><big>(</big><em>func</em><big>)</big><a class="headerlink" href="#contextlib.contextmanager" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is a <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a> that can be used to define a factory
function for <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement context managers, without needing to
create a class or separate <a title="object.__enter__" class="reference external" href="../reference/datamodel.html#object.__enter__"><tt class="xref docutils literal"><span class="pre">__enter__()</span></tt></a> and <a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref docutils literal"><span class="pre">__exit__()</span></tt></a> methods.</p>
<p>A simple example (this is not recommended as a real way of generating HTML!):</p>
<div class="highlight-python"><pre>from contextlib import contextmanager
@contextmanager
def tag(name):
print "<%s>" % name
yield
print "</%s>" % name
>>> with tag("h1"):
... print "foo"
...
<h1>
foo
</h1></pre>
</div>
<p>The function being decorated must return a <a class="reference external" href="../glossary.html#term-generator"><em class="xref">generator</em></a>-iterator when
called. This iterator must yield exactly one value, which will be bound to
the targets in the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement’s <a class="reference external" href="../reference/compound_stmts.html#as"><tt class="xref docutils literal"><span class="pre">as</span></tt></a> clause, if any.</p>
<p>At the point where the generator yields, the block nested in the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a>
statement is executed. The generator is then resumed after the block is exited.
If an unhandled exception occurs in the block, it is reraised inside the
generator at the point where the yield occurred. Thus, you can use a
<a class="reference external" href="../reference/compound_stmts.html#try"><tt class="xref docutils literal"><span class="pre">try</span></tt></a>...<a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a>...<a class="reference external" href="../reference/compound_stmts.html#finally"><tt class="xref docutils literal"><span class="pre">finally</span></tt></a> statement to trap
the error (if any), or ensure that some cleanup takes place. If an exception is
trapped merely in order to log it or to perform some action (rather than to
suppress it entirely), the generator must reraise that exception. Otherwise the
generator context manager will indicate to the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement that
the exception has been handled, and execution will resume with the statement
immediately following the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement.</p>
</dd></dl>
<dl class="function">
<dt id="contextlib.nested">
<tt class="descclassname">contextlib.</tt><tt class="descname">nested</tt><big>(</big><em>mgr1</em><span class="optional">[</span>, <em>mgr2</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#contextlib.nested" title="Permalink to this definition">¶</a></dt>
<dd><p>Combine multiple context managers into a single nested context manager.</p>
<p>Code like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">nested</span>
<span class="k">with</span> <span class="n">nested</span><span class="p">(</span><span class="n">A</span><span class="p">(),</span> <span class="n">B</span><span class="p">(),</span> <span class="n">C</span><span class="p">())</span> <span class="k">as</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">Z</span><span class="p">):</span>
<span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>is equivalent to this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m1</span><span class="p">,</span> <span class="n">m2</span><span class="p">,</span> <span class="n">m3</span> <span class="o">=</span> <span class="n">A</span><span class="p">(),</span> <span class="n">B</span><span class="p">(),</span> <span class="n">C</span><span class="p">()</span>
<span class="k">with</span> <span class="n">m1</span> <span class="k">as</span> <span class="n">X</span><span class="p">:</span>
<span class="k">with</span> <span class="n">m2</span> <span class="k">as</span> <span class="n">Y</span><span class="p">:</span>
<span class="k">with</span> <span class="n">m3</span> <span class="k">as</span> <span class="n">Z</span><span class="p">:</span>
<span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that if the <a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref docutils literal"><span class="pre">__exit__()</span></tt></a> method of one of the nested context managers
indicates an exception should be suppressed, no exception information will be
passed to any remaining outer context managers. Similarly, if the
<a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref docutils literal"><span class="pre">__exit__()</span></tt></a> method of one of the nested managers raises an exception, any
previous exception state will be lost; the new exception will be passed to the
<a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref docutils literal"><span class="pre">__exit__()</span></tt></a> methods of any remaining outer context managers. In general,
<a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref docutils literal"><span class="pre">__exit__()</span></tt></a> methods should avoid raising exceptions, and in particular they
should not re-raise a passed-in exception.</p>
</dd></dl>
<dl class="function">
<dt id="contextlib.closing">
<tt class="descclassname">contextlib.</tt><tt class="descname">closing</tt><big>(</big><em>thing</em><big>)</big><a class="headerlink" href="#contextlib.closing" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a context manager that closes <em>thing</em> upon completion of the block. This
is basically equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">contextmanager</span>
<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">closing</span><span class="p">(</span><span class="n">thing</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">yield</span> <span class="n">thing</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">thing</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>And lets you write code like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">closing</span>
<span class="kn">import</span> <span class="nn">urllib</span>
<span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">'http://www.python.org'</span><span class="p">))</span> <span class="k">as</span> <span class="n">page</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">page</span><span class="p">:</span>
<span class="k">print</span> <span class="n">line</span>
</pre></div>
</div>
<p>without needing to explicitly close <tt class="docutils literal"><span class="pre">page</span></tt>. Even if an error occurs,
<tt class="docutils literal"><span class="pre">page.close()</span></tt> will be called when the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> block is exited.</p>
</dd></dl>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-249"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0343"><strong>PEP 0343</strong></a> - The “with” statement</dt>
<dd>The specification, background, and examples for the Python <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a>
statement.</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="warnings.html"
title="previous chapter">27.5. <tt class="docutils literal docutils literal"><span class="pre">warnings</span></tt> — Warning control</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="abc.html"
title="next chapter">27.7. <tt class="docutils literal docutils literal docutils literal"><span class="pre">abc</span></tt> — Abstract Base Classes</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li><a href="../_sources/library/contextlib.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="abc.html" title="27.7. abc — Abstract Base Classes"
>next</a> |</li>
<li class="right" >
<a href="warnings.html" title="27.5. warnings — Warning control"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="python.html" >27. Python Runtime Services</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="http://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez