Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
075ef1ac
Kaydet (Commit)
075ef1ac
authored
Mar 27, 2006
tarafından
Phillip J. Eby
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Document the "with" statement.
üst
19bf33bc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
1 deletion
+56
-1
ref7.tex
Doc/ref/ref7.tex
+56
-1
No files found.
Doc/ref/ref7.tex
Dosyayı görüntüle @
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