What does '@reify' do and when should it be used? What does '@reify' do and when should it be used? python python

What does '@reify' do and when should it be used?


From the source code documentation:

""" Put the result of a method which uses this (non-data) descriptor decorator in the instance dict after the first call, effectively replacing the decorator with an instance variable."""

A description from from the fuzzy notepad blog sums it up nicely.

It acts like @property, except that the function is only ever called once; after that, the value is cached as a regular attribute. This gives you lazy attribute creation on objects that are meant to be immutable.

So in the code you posted, site_menu can be accesses like a cached property.


According to the doc string (source):

""" Put the result of a method which uses this (non-data)descriptor decorator in the instance dict after the first call,effectively replacing the decorator with an instance variable."""