26

I saw it in the Pyramid tutorial for UX design. I couldn't make out much what this decorator is all about.

Sample code where I saw its usage.

def __init__(self, request):
    self.request = request
    renderer = get_renderer("templates/global_layout.pt")
    self.global_template = renderer.implementation().macros['layout']

@reify
def company_name(self):
    return COMPANY

@reify
def site_menu(self):
    new_menu = SITE_MENU[:]
    url = self.request.url
    for menu in new_menu:
        if menu['title'] == 'Home':
            menu['current'] = url.endswith('/')
        else:
            menu['current'] = url.endswith(menu['href'])
    return new_menu

@view_config(renderer="templates/index.pt")
def index_view(self):
    return {"page_title": "Home"}

@view_config(renderer="templates/about.pt", name="about.html")
def about_view(self):
    return {"page_title": "About"}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48

2 Answers2

40

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.

ditkin
  • 6,774
  • 1
  • 35
  • 37
4

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."""
Jens
  • 8,423
  • 9
  • 58
  • 78
Arka
  • 837
  • 4
  • 8