Add caching to a Python @property while preventing setting and deleting the attribute
I have a method decorated with @property: class Foo: @property def prop(self): pass It prevents modification and deletion, as intended: f = Foo() f.prop = 1 # AttributeError: property 'prop' of 'Foo' object has no setter del f.prop # AttributeError: property 'prop' of 'Foo' object has no deleter Since in my case prop is expensive, I want to cache it. @cached_property is built for this very purpose: from functools import cached_property class Foo: @cached_property def prop(self): pass But it allows setting and deleting the attribute: f = Foo() f.prop = 1 # no error del f.prop # no error How can
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.