Add caching to a Python @property while preventing settattr's and delattr's
I have a method decorated with @property: class Foo: @property def prop(self): pass It prevents modification (setattr) and deletion (delattr), 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 setattr and delattr: f = Foo() f.prop = 1 # no error del f.prop # no error Ho
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.