Property () and pythonproperty of Python decorators

Source: Internet
Author: User

Property () and pythonproperty of Python decorators

1. What is a decoration device?

Official definition: the decorator is a well-known design model that is often used in scenarios with cut-plane requirements. It is more classic in terms of log insertion, performance testing, and transaction processing. The decorator is an excellent design for solving such problems. With the decorator, we can extract a large number of identical codes irrelevant to the function itself and continue to reuse them. In summary, the purpose of the decorator is to add additional functions to existing objects.

Python contains three built-in decorators:

① Staticmethod

② Classmethod

③ Property

For more details, click (portal)

2. Introduction to the property () function

2.1 Why use property?

Generally, when we access attributes and assign values to attributes, we both deal with classes and instance _ dict _. However, if we want to standardize attribute access, there are two methods available: ① data descriptor, ②. property () attribute function.

However, we know that descriptors are relatively complex and difficult for new users to use, so try property (). Compared with the large process of descriptor, property is equivalent to thread.

2.2 function prototype:

Property (fget = None, fset = None, fdel = None, doc = None)

2.3 Definitions of common methods:

Assume that calss Normal has a private Variable _ x, as shown in the following code:

1 # code 1 2 3 class Normal: 4 def _ init _ (self): 5 self. _ x = None 6 def getx (self): 7 return self. _ x 8 def setx (self, value): 9 self. _ x = value10 def delx (self): 11 del self. _ x12 13 14 tN = Normal () 15 print (tN. _ count) 16 17 # output result (error reported) 18 Traceback (most recent call last): 19 File "C: /Users/Administrator/AppData/Local/Programs/Python/Python35/property. py ", line 15, in <module> 20 print (tN. _ count) 21 AttributeError: 'normal' object has no attribute '_ count'

Why is the error reported? Because the _ x attribute of instance tN is private and cannot be accessed directly, we can only call internally defined methods;

1 tN = Normal () 2 tN. setx (10) 3 print (tN. getx () 4 5 # output result: 6 10

Well, using internal methods, you can easily obtain private attribute values of instances or classes;

However, if I change the setx Method Name of class Normal to another one (such as Normal_setx), this function is used in many external places, do I need to find the call location of the method one by one and then change it one by one?

C language may be used, but Python, an intelligent language, cannot solve such a problem?

So, how can we solve the above problems?

Haha, there are actually two ways to do this. Let me hear it slowly!

Method 1: use the property Function property ()

1 # improvement method 2 3 class Normal: 4 def _ init _ (self): 5 self. _ x = None 6 def getx (self): 7 print ('getx (): self. _ x = ', self. _ x) 8 return self. _ x 9 def setx (self, value): 10 self. _ x = value11 print ('setx () ') 12 def delx (self): 13 print ('delx ()') 14 del self. _ x15 16 y = property (getx, setx, delx, "I'm a property") 17 18 19 tN = Normal () 20 tN. y = 1021 tN. y22 del tN. y23 24 # output result: 25 setx () 26 getx (): self. _ x = 1027 delx ()

Haha, the method is directly used as an attribute, so it is a bad thing!

Method 2: Use the @ property modifier

1 # improvement method 2 2 3 class Normal: 4 5 def _ init _ (self): 6 self. _ x = None 7 8 @ property 9 def xx (self): 10 print ('getx (): self. _ x = ', self. _ x) 11 return self. _ x12 13 @ xx. setter14 def xx (self, value): 15 self. _ x = value16 print ('setx () ') 17 18 @ xx. deleter19 def xx (self): 20 print ('delx () ') 21 del self. _ x22 23 24 tN = Normal () 25 tN. xx = 1026 tN. xx27 del tN. xx28 29 # output result: 30 setx () 31 getx (): self. _ x = 1032 delx ()

Haha, how about it? The same result as method 1 is output, proving that both methods are feasible (note that the first one must be @ property (replace getter, otherwise, an error is reported )).

 

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.