问题
While looking through the webapp2 documentation online, I found information on the decoratorwebapp2.cached_property (which can be found at https://webapp-improved.appspot.com/api/webapp2.html#webapp2.cached_property).
In the documentation, it says:
A decorator that converts a function into a lazy property.
My question is:
- What is a lazy property?
Thanks!
回答1:
It is a property decorator that gets out of the way after the first call. It allows you to auto-cache a computed value.
The standard library @property decorator is a data descriptor object and is always called, even if there is an attribute on the instance of the same name.
The @cached_property decorator on the other hand, only has a __get__ method, which means that it is not called if there is an attribute with the same name already present. It makes use of this by setting an attribute with the same name on the instance on the first call.
Given a @cached_property-decorated bar method on an instance named foo, this is what happens:
Python resolves
foo.bar. Nobarattribute is found on the instance.Python finds the
bardescriptor on the class, and calls__get__on that.The
cached_property__get__method calls the decoratedbarmethod.The
barmethod calculates something, and returns the string'spam'.The
cached_property__get__method takes the return value and sets a new attributebaron the instance;foo.bar = 'spam'.The
cached_property__get__method returns the'spam'return value.If you ask for
foo.baragain, Python finds thebarattribute on the instance, and uses that from here on out.
Also see the source code for the original Werkzeug implementation:
# implementation detail: this property is implemented as non-data # descriptor. non-data descriptors are only invoked if there is # no entry with the same name in the instance's __dict__. # this allows us to completely get rid of the access function call # overhead. If one choses to invoke __get__ by hand the property # will still work as expected because the lookup logic is replicated # in __get__ for manual invocation.
来源:https://stackoverflow.com/questions/24704147/python-what-is-a-lazy-property