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
d798a181
Kaydet (Commit)
d798a181
authored
Nis 25, 2006
tarafından
Andrew M. Kuchling
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Rework context terminology
üst
b33842ac
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
21 deletions
+22
-21
whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+22
-21
No files found.
Doc/whatsnew/whatsnew25.tex
Dosyayı görüntüle @
d798a181
...
...
@@ -667,13 +667,13 @@ A high-level explanation of the context management protocol is:
\begin{itemize}
\item
The expression is evaluated and should result in an object
with a
\method
{__
context
__
()
}
method (called a ``context
specifi
er'').
with a
\method
{__
context
__
()
}
method (called a ``context
manag
er'').
\item
The context specifier's
\method
{__
context
__
()
}
method is called,
and must return another object (called a ``
context manager
'') that has
and must return another object (called a ``
with-statement context object
'') that has
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods.
\item
The context
manager
's
\method
{__
enter
__
()
}
method is called. The value
\item
The context
object
's
\method
{__
enter
__
()
}
method is called. The value
returned is assigned to
\var
{
VAR
}
. If no
\code
{
'as
\var
{
VAR
}
'
}
clause
is present, the value is simply discarded.
...
...
@@ -685,7 +685,8 @@ with the exception's information, the same values returned by
\function
{
sys.exc
_
info()
}
. The method's return value controls whether
the exception is re-raised: any false value re-raises the exception,
and
\code
{
True
}
will result in suppressing it. You'll only rarely
want to suppress the exception; the author of the code containing the
want to suppress the exception, because if you do
the author of the code containing the
'
\keyword
{
with
}
' statement will never realize anything went wrong.
\item
If
\var
{
BLOCK
}
didn't raise an exception,
...
...
@@ -724,14 +725,14 @@ First, the \class{DatabaseConnection} needs a \method{__context__()}
method. Sometimes an object can simply return
\code
{
self
}
; the
\module
{
threading
}
module's lock objects do this, for example. For
our database example, though, we need to create a new object; I'll
call this class
\class
{
DatabaseContext
Mgr
}
. Our
\method
{__
context
__
()
}
call this class
\class
{
DatabaseContext
}
. Our
\method
{__
context
__
()
}
method must therefore look like this:
\begin{verbatim}
class DatabaseConnection:
...
def
__
context
__
(self):
return DatabaseContext
Mgr
(self)
return DatabaseContext(self)
# Database interface
def cursor (self):
...
...
@@ -742,12 +743,12 @@ class DatabaseConnection:
"Rolls back current transaction"
\end{verbatim}
Instance
of
\class
{
DatabaseContextMgr
}
need the connection object so that
Instance
s of
\class
{
DatabaseContext
}
need the connection object so that
the connection object's
\method
{
commit()
}
or
\method
{
rollback()
}
methods can be called:
\begin{verbatim}
class DatabaseContext
Mgr
:
class DatabaseContext:
def
__
init
__
(self, connection):
self.connection = connection
\end{verbatim}
...
...
@@ -759,7 +760,7 @@ then add \code{as cursor} to their '\keyword{with}' statement to bind
the cursor to a variable name.
\begin{verbatim}
class DatabaseContext
Mgr
:
class DatabaseContext:
...
def
__
enter
__
(self):
# Code to start a new transaction
...
...
@@ -779,7 +780,7 @@ wished, you could be more explicit and add a \keyword{return}
statement at the marked location.
\begin{verbatim}
class DatabaseContext
Mgr
:
class DatabaseContext:
...
def
__
exit
__
(self, type, value, tb):
if tb is None:
...
...
@@ -798,8 +799,8 @@ The new \module{contextlib} module provides some functions and a
decorator that are useful for writing objects for use with the
'
\keyword
{
with
}
' statement.
The decorator is called
\function
{
context
manager
}
, and lets you write
a si
mple context manager as a generator function
. The generator
The decorator is called
\function
{
context
factory
}
, and lets you write
a si
ngle generator function instead of defining a new class
. The generator
should yield exactly one value. The code up to the
\keyword
{
yield
}
will be executed as the
\method
{__
enter
__
()
}
method, and the value
yielded will be the method's return value that will get bound to the
...
...
@@ -812,9 +813,9 @@ Our database example from the previous section could be written
using this decorator as:
\begin{verbatim}
from contextlib import context
manager
from contextlib import context
factory
@context
manager
@context
factory
def db
_
transaction (connection):
cursor = connection.cursor()
try:
...
...
@@ -831,13 +832,12 @@ with db_transaction(db) as cursor:
\end{verbatim}
You can also use this decorator to write the
\method
{__
context
__
()
}
method for a class without having to create a new class representing
the context manager:
method for a class:
\begin{verbatim}
class DatabaseConnection:
@context
manager
@context
factory
def
__
context
__
(self):
cursor = self.cursor()
try:
...
...
@@ -850,10 +850,11 @@ class DatabaseConnection:
\end{verbatim}
There's a
\function
{
nested(
\var
{
mgr1
}
,
\var
{
mgr2
}
, ...)
}
function that
combines a number of contexts so you don't need to write
nested '
\keyword
{
with
}
' statements. This example statement does two
things, starting a database transaction and acquiring a thread lock:
The
\module
{
contextlib
}
module also has a
\function
{
nested(
\var
{
mgr1
}
,
\var
{
mgr2
}
, ...)
}
function that combines a number of contexts so you
don't need to write nested '
\keyword
{
with
}
' statements. In this
example, the single '
\keyword
{
with
}
' statement both starts a database
transaction and acquires a thread lock:
\begin{verbatim}
lock = threading.Lock()
...
...
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