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
9743e4f6
Kaydet (Commit)
9743e4f6
authored
Ara 16, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Improve the ContextDecorator example.
üst
f68fa05f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
15 deletions
+13
-15
3.2.rst
Doc/whatsnew/3.2.rst
+13
-15
No files found.
Doc/whatsnew/3.2.rst
Dosyayı görüntüle @
9743e4f6
...
...
@@ -818,33 +818,31 @@ definition:
>>> import logging
>>> logging.basicConfig(level=logging.INFO)
>>> @contextmanager
... def track_entry_and_exit():
... logging.info('Ent
ry'
)
... def track_entry_and_exit(
name
):
... logging.info('Ent
ering: {}'.format(name)
)
... yield
... logging.info('Exit
'
)
... logging.info('Exit
ing: {}'.format(name)
)
Formerly, this would have only been usable as a context manager:
>>> with track_entry_and_exit():
>>> with track_entry_and_exit(
'widget loader'
):
... print('Some time consuming activity goes here')
... load_widget()
Now, it can be used as a decorator as well:
>>> @track_entry_and_exit
>>> @track_entry_and_exit
('widget loader')
... def activity():
... print('Some time consuming activity goes here'
... print('Some time consuming activity goes here')
... load_widget()
Trying to fulfill two roles at once places some limitations on the technique.
Context managers normally have the flexibility to return an argument usable by
the :keyword:`with`-statement, and function decorators can be constructed to
accept arguments or to know the name of the function they are enclosing. Since
those features of context managers and function decorators are not overlapping,
those features are not supported.
In the above example, there is not a clean way for the
:func:`track_entry_and_exit` decorator to know the name of the enclosed
function. Likewise, the *track_entry_and_exit* context manager does not have a
way to return a logging instance for use in the body of enclosed statements.
the :keyword:`with`-statement, but there is no parallel for function decorators.
In the above example, there is not a clean way for the *track_entry_and_exit*
context manager does not have a way to return a logging instance for use in the
body of enclosed statements.
(Contributed by Michael Foord in :issue:`9110`.)
...
...
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