Often I see that the methods of the models turn into property. Sometimes this is done through the decorator @property, and sometimes through property (method_name).

What for? After all, from templates they are still called without ().

    1 answer 1

    It is likely that it would be possible to work with the model through the properties not only in the templates, but also, for example, in the view.

    In addition, the @property decorator allows you to visually separate properties from methods.

    In a simplified form, the @property decorator is like the following handle:

    class property(object): def __init__(self, fget): self.fget = fget def __get__(self, obj, type = None): return self.fget(obj) 

    Accordingly, if we have a class:

     class Foo(object): def bar(self): return 'test1'; @property def baz(self): return 'test2'; 

    That, from the point of view of the internal python architecture, calls to the bar() method of the baz property will look like this:

     obj = Foo() # это выражение полностью эквивалентно: obj.bar() Foo.__dict__['bar'](obj) # а это полностью эквивалентно: obj.baz Foo.__dict__['baz'].__get__(obj) 

    So When using properties, we will have: 1 additional method call __get__() . Those. almost none.

    • Is there a chance that the template builder works faster? - rnd_d
    • one
      Updated the answer - Ilya Pirogov
    • thanks, of course, there will be no acceleration. - rnd_d