#30864: Document and endorse django.utils.decorators.classproperty -------------------------------------+------------------------------------- Reporter: jgonggrijp | Owner: nobody Type: New feature | Status: new Component: Documentation | Version: 2.2 Severity: Normal | Resolution: Keywords: utils classproperty | Triage Stage: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 1 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Simon Charette): > Personally, I'd love having `cached_classproperty`… That would be hard to implement without some metaclass mockery or relying on some naive `if not hasattr(cls, '_cached_foo')` constructs. `cached_property` is efficient because it relies on the descriptor protocol to cache its return value into `__dict__` and avoid class level attribute lookups. We can't achieve similar things without defining the attribute on the class of the class (metaclass) and use the class's `__dict__` as a cache store. I guess the following could work. {{{#!python class cached_classproperty: NOT_SET = object() def __init__(self, method): self.method = method self.cached_value = self.NOT_SET def __get__(self, instance, owner): if self.cached_value is not self.NOT_SET: return self.cached_value self.cached_value = self.method(owner) return self.cached_value def __set__(self, instance, value): self.cached_value = value def __delete__(self, instance): self.cached_value = self.NOT_SET }}} But this wouldn't be as efficient as `cached_property` because `__get__` would always be called. Anyway the `cached_classproperty` case should probably be discussed in another ticket or on the mailing list. -- Ticket URL: <https://code.djangoproject.com/ticket/30864#comment:2> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.0b552d90a7607ea951cf0ed3704801bc%40djangoproject.com.