Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
304f9ff7
Commit
304f9ff7
authored
Jul 29, 2007
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify PEP 343 description.
parent
8eab424f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
9 deletions
+18
-9
Doc/whatsnew/whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+18
-9
No files found.
Doc/whatsnew/whatsnew25.tex
View file @
304f9ff7
...
...
@@ -640,15 +640,20 @@ with expression [as variable]:
\end{verbatim}
The expression is evaluated, and it should result in an object that
supports the context management protocol. This object may return a
value that can optionally be bound to the name
\var
{
variable
}
. (Note
carefully that
\var
{
variable
}
is
\emph
{
not
}
assigned the result of
\var
{
expression
}
.) The object can then run set-up code
before
\var
{
with-block
}
is executed and some clean-up code
is executed after the block is done, even if the block raised an exception.
supports the context management protocol (that is, has
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods.
To enable the statement in Python 2.5, you need
to add the following directive to your module:
The object's
\method
{__
enter
__
()
}
is called before
\var
{
with-block
}
is
executed and therefore can run set-up code. It also may return a value
that is bound to the name
\var
{
variable
}
, if given. (Note carefully
that
\var
{
variable
}
is
\emph
{
not
}
assigned the result of
\var
{
expression
}
.)
After execution of the
\var
{
with-block
}
is finished, the object's
\method
{__
exit
__
()
}
method is called, even if the block raised an exception,
and can therefore run clean-up code.
To enable the statement in Python 2.5, you need to add the following
directive to your module:
\begin{verbatim}
from
__
future
__
import with
_
statement
...
...
@@ -668,9 +673,13 @@ with open('/etc/passwd', 'r') as f:
\end{verbatim}
After this statement has executed, the file object in
\var
{
f
}
will
have been automatically closed, even if the
'for'
loop
have been automatically closed, even if the
\keyword
{
for
}
loop
raised an exception part-way through the block.
\note
{
In this case,
\var
{
f
}
is the same object created by
\function
{
open()
}
, because
\method
{
file.
__
enter
__
()
}
returns
\var
{
self
}
.
}
The
\module
{
threading
}
module's locks and condition variables
also support the '
\keyword
{
with
}
' statement:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment