mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Fix an error in the last contextlib.closing example
This commit is contained in:
parent
9cc3b1ccef
commit
790c3c1377
1 changed files with 3 additions and 3 deletions
|
@ -149,8 +149,7 @@ occurs, \code{page.close()} will be called when the \keyword{with}
|
||||||
block is exited.
|
block is exited.
|
||||||
|
|
||||||
Context managers with a close method can use this context factory
|
Context managers with a close method can use this context factory
|
||||||
directly without needing to implement their own
|
to easily implement their own \method{__context__()} method.
|
||||||
\method{__context__()} method.
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
@ -158,7 +157,8 @@ from contextlib import closing
|
||||||
class MyClass:
|
class MyClass:
|
||||||
def close(self):
|
def close(self):
|
||||||
print "Closing", self
|
print "Closing", self
|
||||||
__context__ = closing
|
def __context__(self):
|
||||||
|
return closing(self)
|
||||||
|
|
||||||
>>> with MyClass() as x:
|
>>> with MyClass() as x:
|
||||||
... print "Hello from", x
|
... print "Hello from", x
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue