Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
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
django
Commits
7e6af9d4
Kaydet (Commit)
7e6af9d4
authored
Agu 08, 2013
tarafından
Daniele Procida
Kaydeden (comit)
Tim Graham
Agu 08, 2013
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added more on @cached_property, refs #20870
üst
1c4a9bd9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
4 deletions
+18
-4
utils.txt
docs/ref/utils.txt
+18
-4
No files found.
docs/ref/utils.txt
Dosyayı görüntüle @
7e6af9d4
...
...
@@ -434,8 +434,9 @@ Atom1Feed
.. class:: cached_property(object)
The ``@cached_property`` decorator caches the result of a method with a
single ``self`` argument as a property. The cached result will persist as
long as the instance does.
single ``self`` argument as a property. The cached result will persist
as long as the instance does, so if the instance is passed around and the
function subsequently invoked, the cached result will be returned.
Consider a typical case, where a view might need to call a model's method
to perform some computation, before placing the model instance into the
...
...
@@ -455,7 +456,7 @@ Atom1Feed
# in the template:
{% for friend in person.friends %}
``friends()`` will be called twice. Since the instance ``person`` in
Here,
``friends()`` will be called twice. Since the instance ``person`` in
the view and the template are the same, ``@cached_property`` can avoid
that::
...
...
@@ -473,7 +474,20 @@ Atom1Feed
# in the view:
if person.friends:
You may clear the cached result using ``del person.friends``.
The cached value can be treated like an ordinary attribute of the instance::
# clear it, requiring re-computation next time it's called
del person.friends # or delattr(person, "friends")
# set a value manually, that will persist on the instance until cleared
person.friends = ["Huckleberry Finn", "Tom Sawyer"]
As well as offering potential performance advantages, ``@cached_property``
can ensure that an attribute's value does not change unexpectedly over the
life of an instance. This could occur with a method whose computation is
based on ``datetime.now()``, or simply if a change were saved to the
database by some other process in the brief interval between subsequent
invocations of a method on the same instance.
.. function:: allow_lazy(func, *resultclasses)
...
...
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