1. What is an adorner?
Official definition: The adorner is a well-known design pattern, which is often used in scenes where there is a demand for facets, with the classic insert log, performance test, transaction processing, and so on. 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
For a more detailed explanation, please click (Portal)
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:
1 #Code 12 3 classNormal:4 def __init__(self):5Self.__x=None6 defGetx (self):7 returnSelf.__x8 defsetx (self, value):9Self.__x=valueTen defDelx (self): One delSelf.__x A - -TN =Normal () the Print(TN.__count) - - #output result (Error) - Traceback (most recent): +File"c:/users/administrator/appdata/local/programs/python/python35/property.py", line 15,inch<module> - Print(TN.__count) +Attributeerror:'Normal'object has 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;
1 tN = Normal ()2 tn.setx (ten)3print(Tn.getx ()) 4 5 # Output Result: 6 10
Well, using an internal method, you can easily get the value of the private property of the instance or class;
However, if I was in the mood for that day, I changed the Setx method name of class Normal to something else (like Normal_setx), and I used the function in many places outside, did I need one to find the calling location of the method, and then change it one by one?
C language may be, but Python, an intelligent language, how can such a thing can't solve it?
So, how to solve the above please?
Haha, in fact there are two ways oh, listen to my slow way to Oh!
Method One: Using Property function Properties ()
1 #Improvement Method One2 3 classNormal:4 def __init__(self):5Self.__x=None6 defGetx (self):7 Print('getx (): self.__x=', self.__x)8 returnSelf.__x9 defsetx (self, value):TenSelf.__x=value One Print('setx ()') A defDelx (self): - Print('Delx ()') - delSelf.__x the -Y = Property (Getx, Setx, Delx,"I ' m a property") - - +tn=Normal () -tn.y=10 + TN.Y A delTN.Y at - #Output Result: - setx () -Getx (): Self.__x= 10 -Delx ()
Haha, directly to the method as a property to operate, good bad looks!
Method Two: The use of @property decorative device
1 #Improvement Method Two2 3 classNormal:4 5 def __init__(self):6Self.__x=None7 8 @property9 defxx (self):Ten Print('getx (): self.__x=', self.__x) One returnSelf.__x A - @xx. Setter - defxx (self, value): theSelf.__x=value - Print('setx ()') - - @xx. Deleter + defxx (self): - Print('Delx ()') + delSelf.__x A at -tn=Normal () -tn.xx=10 - tn.xx - deltn.xx - in #output result information: - setx () toGetx (): Self.__x= 10 +Delx ()
Haha, how, with the output of the method of the same results, proved that both methods are feasible (note oh, the first must be @property (substitute getter Oh, otherwise will be error)).
The property of the Python decorator ()