Dispose pattern: Difference between revisions

Content deleted Content added
FireflyBot (talk | contribs)
m (BOT) Substituting Template:Date - should not be used in article space.
Line 120:
</syntaxhighlight>
 
Similarly, the [[Python (programming language)|Python]] language has a <code>with</code> statement that can be used to similar effect with a ''context manager'' object. The ''context manager protocol'' requires implementing <code>__enter__</code> and <code>__exit__</code> methods which get automatically called by the <code>with</code> statement construct, to prevent duplication of code that would otherwise occur with the <code>try</code>/<code>finally</code> pattern.<ref>{{cite web |author=[[Guido van Rossum]], Nick Coghlan |date={{date|2011-06-13}} June 2011 |title=PEP 343: The "with" Statement |publisher=Python Software Foundation |url=http://legacy.python.org/dev/peps/pep-0343/ }}</ref>
<syntaxhighlight lang="python">
with resource_context_manager() as resource: