Python - What is a lazy property?

Looking through the online documentation of webapp2, I found information about the decorator webapp2.cached_property(which can be found at https://webapp-improved.appspot.com/api/webapp2.html#webapp2.cached_property ).

The documentation says:

A decorator that converts a function into a lazy property.

My question is:

  • What is a lazy property?

Thank!

+4
source share
1 answer

This is a decorator propertythat gets out of the way after the first call. It allows you to automatically cache the calculated value.

@property decorator - , .

@cached_property, , __get__, , , . , .

@cached_property -decorated bar foo, :

  • Python foo.bar. bar .

  • Python bar __get__.

  • cached_property __get__ bar.

  • bar - 'spam'.

  • cached_property __get__ bar ; foo.bar = 'spam'.

  • cached_property __get__ 'spam'.

  • foo.bar, Python bar .

. Werkzeug:

# 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 __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.
+2

Source: https://habr.com/ru/post/1548114/


All Articles