#35745: Improve Documentation on Clearing Cached Property Values -------------------------------+----------------------------------------- Reporter: Jae Hyuck Sa | Owner: Jae Hyuck Sa Type: Uncategorized | Status: closed Component: Documentation | Version: 5.1 Severity: Normal | Resolution: invalid Keywords: | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+----------------------------------------- Comment (by Jae Hyuck Sa ):
Replying to [comment:3 Sarah Boyce]: > From what I understand, I think this is documented: https://docs.djangoproject.com/en/5.1/ref/utils/#django.utils.functional.cached_property > > > The cached value can be treated like an ordinary attribute of the instance: > > > > {{{#!python > > # 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"] > > }}} I appreciate the feedback. While the current documentation mentions using del to clear cached properties, this approach can raise an AttributeError if the property is not already cached. A more robust pattern, extensively used in the Django source code, involves: {{{ some_class_instance.__dict__.pop("some_cached_property", None) }}} This method avoids potential errors and ensures a smoother experience for developers. Given that the current documentation acknowledges the possibility of raising an AttributeError, would it be possible to update the documentation to also recommend this more robust method to the readers? -- Ticket URL: <https://code.djangoproject.com/ticket/35745#comment:4> 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/01070191d5aae744-8afa2d6f-777a-46c8-aad8-cf8d58961f3b-000000%40eu-central-1.amazonses.com.