Python Decorator's property () tutorial

Source: Internet
Author: User
Tags access properties python decorator

1. What is an adorner?

Official definition: adorners are a well-known design pattern and are often used for scenes with cut-off requirements, with more classic insert logs, performance tests, transaction processing, and more. Decorators are a great design for solving such problems, and with adorners, we can pull out a lot of the same code that is not relevant to the function itself and continue to reuse it. In summary, the function of an adorner is to add additional functionality to an already existing object.

A total of three built-in adorners are included in Python:

①staticmethod

②classmethod

③property

2. Attribute Function property () talking about

2.1 Why should I use property?

In general, we deal with classes and instance __dict__ when we access properties and assign values to them, but if we want to standardize property access, there are two ways to do this: the ① data descriptor, the ② property () attribute function.

However, we know that the descriptor is relatively complex, for beginners, it is very difficult to use, then you might as well try the property (), relative to the descriptor of this large process, the property is equivalent to a thread.

2.2 Function Prototypes:

Property (Fget=none, Fset=none, Fdel=none, Doc=none)

2.3 Common Method Definitions:

Suppose there is a private variable __x in Calss Normal, as shown in the following code:

#code 1class Normal:    def __init__ (self):        self.__x = None    def getx (self):        return self.__x    def setx ( Self, value):        self.__x = value    def delx (self):        del Self.__xtn = Normal () print (Tn.__count)

Output result (Error)

Traceback (most recent):  File "c:/users/administrator/appdata/local/programs/python/python35/ property.py ", line <module>    print (tn.__count) attributeerror: ' Normal ' object have no attribute ' __count '

Why the error? Since the attribute of the instance TN is __x as a private property and cannot be accessed directly, we can only invoke internally defined methods;

TN = Normal () tn.setx () print (Tn.getx ())

Output Result:

6 10

Using the internal method, it is easy to get the private property value of the instance or class;

However, if I want to change the Setx method name of class Normal to something else (such as normal_setx), the function is used in many places outside, is it necessary that I need one to find the calling location of the method, and then change it one by one?

C language May, but Python, a high-level language, how can such a thing is not solved?

So, how to solve the above problem?

There are actually two ways.

Method One: Use Attribute Function property ()

Class Normal:    def __init__ (self):        self.__x = None    def getx (self):        print (' Getx (): self.__x= ', self.__x )        return self.__x    def setx (self, value):        self.__x = value        print (' setx () ')    def delx (self):        Print (' Delx () ')        del self.__x    y = property (Getx, Setx, Delx, "I ' m a Property") Tn=normal () Tn.y=10tn.ydel tn.y# Output: Setx () Getx (): self.__x= 10delx ()

It is very convenient to manipulate the method as a property directly.

Method Two: Use @property adorner

Class Normal:        def __init__ (self):        self.__x = None    @property    def xx (self):        print (' Getx (): self.__ X= ', self.__x)        return self.__x        @xx. Setter    def xx (self, value):        self.__x = value        print (' setx () ')    @xx. deleter    def xx (self):        print (' Delx () ')        del self.__xtn=normal () Tn.xx=10tn.xxdel tn.xx# Output result information: Setx () Getx (): self.__x= 10delx ()

The same result as the output of method one proves that both methods are feasible (note: The first must be @property (substitute getter Oh, otherwise it will be an error)).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.