Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pyramid

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"} 
like image 692
Sushant Gupta Avatar asked May 27 '12 18:05

Sushant Gupta


People also ask

How do you use reify?

People might reify aspects of their situation. He differed with metaphysicians because they reified words and treated them as if they were realities, and he objected to materialism because it ignored or overlooked the importance of form.

What does reify mean programming?

Reification is the process by which an abstract idea about a computer program is turned into an explicit data model or other object created in a programming language. A computable/addressable object—a resource—is created in a system as a proxy for a non computable/addressable object.

What is reification with an example?

Reification is a complex idea for when you treat something immaterial — like happiness, fear, or evil — as a material thing. This can be a way of making something concrete and easier to understand, like how a wedding ring is the reification of a couple's love.

What does reification mean?

noun. the act of treating something abstract, such as an idea, relation, system, quality, etc., as if it were a concrete object: Defining “home” as if it were just a roof over one's head, instead of the center of a web of relationships, leads in turn to the reification of homelessness.


1 Answers

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.

like image 198
ditkin Avatar answered Oct 14 '22 08:10

ditkin