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
075ef1ac
Commit
075ef1ac
authored
Mar 27, 2006
by
Phillip J. Eby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document the "with" statement.
parent
19bf33bc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
1 deletion
+56
-1
Doc/ref/ref7.tex
Doc/ref/ref7.tex
+56
-1
No files found.
Doc/ref/ref7.tex
View file @
075ef1ac
...
...
@@ -46,6 +46,7 @@ Summarizing:
\productioncont
{
|
\token
{
while
_
stmt
}}
\productioncont
{
|
\token
{
for
_
stmt
}}
\productioncont
{
|
\token
{
try
_
stmt
}}
\productioncont
{
|
\token
{
with
_
stmt
}}
\productioncont
{
|
\token
{
funcdef
}}
\productioncont
{
|
\token
{
classdef
}}
\production
{
suite
}
...
...
@@ -311,8 +312,62 @@ statement to generate exceptions may be found in section~\ref{raise}.
\section
{
The
\keyword
{
with
}
statement
\label
{
with
}}
\stindex
{
with
}
The
\keyword
{
with
}
statement specifies
The
\keyword
{
with
}
statement is used to wrap the execution of a block
with methods defined by a context manager (see
section~
\ref
{
context-managers
}
). This allows common
\keyword
{
try
}
...
\keyword
{
except
}
...
\keyword
{
finally
}
usage patterns to
be encapsulated as context managers for convenient reuse.
\begin{productionlist}
\production
{
with
_
stmt
}
{
"with"
\token
{
expression
}
["as" target
_
list] ":"
\token
{
suite
}}
\end{productionlist}
The execution of the
\keyword
{
with
}
statement proceeds as follows:
\begin{enumerate}
\item
The expression is evaluated, to obtain a context manager
object.
\item
The context manager's
\method
{__
context
__
()
}
method is invoked to
obtain a context object.
\item
The context object's
\method
{__
enter
__
()
}
method is invoked.
\item
If a target list was included in the
\keyword
{
with
}
statement, the return value from
\method
{__
enter
__
()
}
is assigned to it.
\note
{
The
\keyword
{
with
}
statement guarantees that if the
\method
{__
enter
__
()
}
method returns without an error, then
\method
{__
exit
__
()
}
will always be called. Thus, if an error occurs
during the assignment to the target list, it will be treated the same as
an error occurring within the suite would be. See step 6 below.
}
\item
The suite is executed.
\item
The context object's
\method
{__
exit
__
()
}
method is invoked. If an
exception caused the suite to be exited, its type, value, and
traceback are passed as arguments to
\method
{__
exit
__
()
}
. Otherwise,
three
\constant
{
None
}
arguments are supplied.
If the suite was exited due to an exception, and the return
value from the
\method
{__
exit
__
()
}
method was false, the exception is
reraised. If the return value was true, the exception is suppressed, and
execution continues with the statement following the
\keyword
{
with
}
statement.
If the suite was exited for any reason other than an exception, the
return value from
\method
{__
exit
__
()
}
is ignored, and execution proceeds
at the normal location for the kind of exit that was taken.
\end{enumerate}
\begin{seealso}
\seepep
{
0343
}{
The "with" statement
}
{
The specification, background, and examples for the
Python
\keyword
{
with
}
statement.
}
\end{seealso}
\section
{
Function definitions
\label
{
function
}}
\indexii
{
function
}{
definition
}
...
...
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