Home  >  Article  >  Backend Development  >  Detailed explanation of examples of using Python delayed initialization to improve performance

Detailed explanation of examples of using Python delayed initialization to improve performance

Y2J
Y2JOriginal
2017-04-17 18:01:361348browse

The so-called delayed calculation of class attributes is to define the attributes of the class as a property, which will only be calculated when accessed, and once accessed, the result will be cached, without having to do it every time All calculated. The main purpose of constructing a delayed calculation property is to improve performance

property

Before getting to the point, let’s understand the usage of property. Property can convert property access into method invocation.

class Circle(object): 
  def init(self, radius): 
    self.radius = radius 
  
  @property
  def area(self): 
    return 3.14 * self.radius ** 2
  
c = Circle(4) 
print c.radius 
print c.area

It can be seen that although area is defined as a method, after adding @property, c.area can be directly executed and accessed as a property.

Now the question is, every time c.area is called, it will be calculated once, which is a waste of CPU. How can we only calculate it once? This is lazy property

Code implementation

class LazyProperty(object):
  def init(self, func):
    self.func = func
  def get(self, instance, owner):
    if instance is None:
      return self
    else:
      value = self.func(instance)
      setattr(instance, self.func.name, value)
      return value
import math
class Circle(object):
  def init(self, radius):
    self.radius = radius
  @LazyProperty
  def area(self):
    print 'Computing area'
    return math.pi * self.radius ** 2
  @LazyProperty
  def perimeter(self):
    print 'Computing perimeter'
    return 2 * math.pi * self.radius

Description

Defines a delayed calculation decorator class LazyProperty. Circle is a class used for testing. The Circle class has three attributes: radius, area, and perimeter. The properties of area and perimeter are decorated by LazyProperty. Let's try the magic of LazyProperty:

>>> c = Circle(2)
>>> print c.area
Computing area
12.5663706144
>>> print c.area
12.5663706144

In area(), "Computing area" will be printed once every time it is calculated, and c.area is called twice in a row. The last "Computing area" is only printed once. This is due to LazyProperty, as long as it is called once, it will not be counted again no matter how many subsequent calls are made.

The above is the detailed content of Detailed explanation of examples of using Python delayed initialization to improve performance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn