Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20870 closed New feature (fixed)

django.utils.functional.cached_property should be documented

Reported by: Daniele Procida Owned by: nobody
Component: Documentation Version: 1.5
Severity: Normal Keywords:
Cc: info@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It's a very common pattern in Django to find oneself calling a method in a view to check something - if MyModel.mymethod(): - then a moment later in the template {% for things in mymodel.mymethod %} - so mymethod() gets called twice.

If mymethod()` is very expensive, this could be a problem.

django.utils.functional.cached_property already exists and solves the problem. Doesn't it deserve to be documented so that we can all enjoy it?

https://github.com/django/django/blob/master/django/utils/functional.py#L38

Change History (11)

comment:1 by Markus Holtermann, 11 years ago

Cc: info@… added

comment:2 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

django-developers thread asking if OK to document this has received a couple +1's with a caveat from Aymeric: "The main drawback of the current implementation is that it's impossible to clear the cached value. That should be mentioned in the docs."

comment:3 by Anssi Kääriäinen, 11 years ago

It doesn't seem to be impossible:

from django.utils.functional import cached_property


class Foobar(object):
    i = 0

    @cached_property
    def foo(self):
        Foobar.i += 1
        return Foobar.i

f = Foobar()
print f.foo
print f.foo
del f.foo
print f.foo
print f.foo
OUT:
1
1
2
2

If we want to document that is a different thing. That forces the implementation to have the cached value in self.__dict__, but on the other hand that seems to be the only way to implement cached_property...

comment:4 by Daniele Procida, 11 years ago

https://github.com/django/django/pull/1449 (closed; left here for reference only)

New pull request: https://github.com/django/django/pull/1452

Last edited 11 years ago by Daniele Procida (previous) (diff)

comment:5 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 7a2296eb5bb05a4109392f8333e934d576d79d35:

Fixed #20870 -- Documented django.utils.functional.cached_property

comment:6 by Tim Graham <timograham@…>, 11 years ago

In e8ea55212bc1520da2e9b87af9990505c5baa000:

[1.5.x] Fixed #20870 -- Documented django.utils.functional.cached_property

Backport of 7a2296eb5b from master

comment:7 by Tim Graham <timograham@…>, 11 years ago

In 25ce1e0e0a37d7be7ceff1e37bcb9ebd78a959d9:

[1.6.x] Fixed #20870 -- Documented django.utils.functional.cached_property

Backport of 7a2296eb5b from master

comment:9 by Tim Graham <timograham@…>, 11 years ago

In 7e6af9d40ce0232deb9d4c6943beef0b62a20a08:

Added more on @cached_property, refs #20870

comment:10 by Tim Graham <timograham@…>, 11 years ago

In bf55bbdcb55b830e3d34b07a067584bce4e6f158:

[1.5.x] Added more on @cached_property, refs #20870

Backport of 7e6af9d40c from master

comment:11 by Tim Graham <timograham@…>, 11 years ago

In 9cc7407f2c6b457c1b8f1e7c2a57e7b065d9bc41:

[1.6.x] Added more on @cached_property, refs #20870

Backport of 7e6af9d40c from master

Note: See TracTickets for help on using tickets.
Back to Top