Tricky usage of @property decorator in Python (code example)

不言
Release: 2018-11-23 16:45:37
forward
1783 people have browsed it

This article brings you the technical usage (code examples) of the @property decorator in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

@property decorator can turn a method into a property and call it. Let’s take a look at Python’s black magic @property decorator usage skills analysis

@ What are attributes used for? On the surface, it seems that a method is accessed as an attribute.

The above code

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
Copy after login

As you can see, although the area is defined in the form of a method, after adding @property, it can be accessed directly c.area as a property.

Now the question comes. Every time c.area is called, it will be calculated once. It is a waste of CPU. How can it be calculated only once? This is the property of laziness.

class lazy(object): 
  def __init__(self, func): 
    self.func = func 
  
  def __get__(self, instance, cls): 
    val = self.func(instance) 
    setattr(instance, self.func.__name__, val) 
    return val 
  
class Circle(object): 
  def __init__(self, radius): 
    self.radius = radius 
  
  @lazy 
  def area(self): 
    print 'evalute' 
    return 3.14 * self.radius ** 2 
  
c = Circle(4) 
print c.radius 
print c.area 
print c.area 
print c.area
Copy after login

As you can see, 'evalute' is only output once, so you should have a good understanding of @Lazy's mechanism.

Here, the lazy class has a __get__ method, which indicates that it is a descriptor. When c.area is executed for the first time, due to order issues, it is first searched in Ç.__dict__. If it is not found, just Go to the class space to find it. In the class circle, there is the area() method, so it is intercepted by __get__.

In __get__, call the region() method of the instance to calculate the result, and dynamically add an attribute with the same name to the instance and assign the result to it, that is, add it to Ç.__ dict__.

When executing c.area again, first go to Ç.__ dict__ to find it, because it is already there at this time, so it will not go through the area () method and __get__.

Attention

Please pay attention to the following code scenarios:

Code snippet 1:

class Parrot(object): 
  def __init__(self): 
    self._voltage = 100000 
  
  @property 
  def voltage(self): 
    """Get the current voltage.""" 
    return self._voltage 
  
if __name__ == "__main__": 
  # instance 
  p = Parrot() 
  # similarly invoke "getter" via @property 
  print p.voltage 
  # update, similarly invoke "setter" 
  p.voltage = 12
Copy after login

Code snippet 2:

class Parrot: 
  def __init__(self): 
    self._voltage = 100000 
  
  @property 
  def voltage(self): 
    """Get the current voltage.""" 
    return self._voltage 
  
if __name__ == "__main__": 
  # instance 
  p = Parrot() 
  # similarly invoke "getter" via @property 
  print p.voltage 
  # update, similarly invoke "setter" 
  p.voltage = 12
Copy after login

Code 1 ,The difference between 2 is

class Parrot (object):

Under python2, run the test separately

Fragment 1: An expected error message AttributeError: Unable to be prompted Set properties

Fragment 2: Correct operation

Refer to the python2 documentation. @property will provide a ready-only attribute. The above code does not provide the corresponding @voltage.setter. It stands to reason that the code in Fragment 2 will Prompt running error, in the python2 documentation, we can find the following information:

BIF:

property([fget[,fset[,fdel[,doc]]]])

Returns the attribute properties of the new style class (classes derived from Object).

It turns out that under python2, the built-in type object is not the default base class. If there is no clear explanation when defining the class (code snippet 2), the Parrot we defined (code snippet 2) will not inherit Object

The object class just provides the @property function we need. We can find the following information in the document:

New style lesson

Any class that inherits from object . This includes all built-in types such as list and dict. Only new-style classes can use Python's newer, generic features such as __slots__, descriptors, attributes and __getattribute__().

At the same time, we can also verify through the following method

class A: 
  pass 
>>type(A) 
<type &#39;classobj&#39;>
Copy after login
class A(object): 
  pass 
>>type(A) 
<type &#39;type&#39;>
Copy after login

From the returned , we can see that is the object type we need (python 3.0 uses the object class as the default base class, so all will return )

In order to consider the compatibility of the python version of the code during the transition period Question, I think when defining a class file, you should explicitly define the object as a good habit

The final code will be as follows:

class Parrot(object): 
  def __init__(self): 
    self._voltage = 100000 
  @property 
  def voltage(self): 
    """Get the current voltage.""" 
    return self._voltage 
  @voltage.setter 
  def voltage(self, new_value): 
    self._voltage = new_value 
  
if __name__ == "__main__": 
  # instance 
  p = Parrot() 
  # similarly invoke "getter" via @property 
  print p.voltage 
  # update, similarly invoke "setter" 
  p.voltage = 12
Copy after login

The above is the detailed content of Tricky usage of @property decorator in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template