Attribute (property )
An attribute is an interception of a particular property of a class that, when manipulated, performs a specific function to intercept the operation of the property.
Implementation of features
Attributes are implemented using the property class, or they can be implemented using the properties decorator, which is essentially the same.
The __init__ function of the property class receives 4 parameters to implement the acquisition, assignment, deletion, and documentation of the attributes.
def __init__(Self, Fget=none, Fset=none, Fdel=none, Doc=none):#known special case of property.__init__ """ Property (Fget=none, Fset=none, Fdel=none, Doc=none) and property attribute Fget are a function to be used for getting an attribute value, and likewise fset are a function for setting, and Fdel a function for del ' in g, an attribute. Typical define a managed attribute x:class C (object): Def getx (self): return self._x def setx (self, value): self._x = value def delx (self): del self._x x = property (Getx, set X, Delx, "I ' m the ' X ' property.") Decorators make defining new properties or modifying existing ones Easy:class C (object): @prope Rty def x (self): "I am the ' X ' property." return self._x @x.setter def x (self, value): self._x = value @x.deleter def x (self): del self._x # (copied from class Doc)""" Pass
From the code, 4 parameters are not necessary, if not passed the corresponding operation function, then the default value of None, the corresponding operation is not supported, attempting to invoke the default value of None, throws an exception.
Test code:
classMan (object):def __init__(self, Age): Self._age=Age @propertydefAge (self):returnself._age @age. GetterdefAge (self):returnSelf._ageif __name__=='__main__': Tom= Man (22) Print(tom.age) tom.age= 23Print(Tom.age)
An exception is thrown when an attempt is made to assign a value to age because of a function method that lacks a setter.
Attributeerror:can't set attribute
The age () method adds an @property adorner, which is actually equivalent to age = Property (), which assigns the age value to the instance of the property.
After testing, the
@property def Age (self): Pass
Revision changed to
Age = Property ()
Does not affect the execution of the code, so the @age.setter decorator is well understood, because the age is an instance of the property class, the property class has a setter method, age inherits the property class setter method, so you can use @ Age.setter decorator. Other setters, getter, deleter are also the same.
Inheritance of attributes
Instances and subclasses of a class inherit the attributes of the class and test the code:
class Person (object):
def __init__(self, Age): Self._age=Age @propertydefAge (self):returnself._age @age. SetterdefAge (self, value): Self._age= value * 2@age. GetterdefAge (self):returnSelf._ageclassMan :Passif __name__=='__main__': Tom= Man (22) Print(tom.age) tom.age= 23Print(Tom.age)
In the setter method of age, multiply the value of age by 2, and from the running result, the subclass and its instances are inherited property
2246
Descriptor
Any class with the following method can be used as a descriptor
def __get__ (self, instance, owner): ... def __set__ (self, instance, value): ... def __delete__ (self, instance): ....
The __get__ method receives three parameters: Self is the descriptor instance itself, and instance refers to the instance to which the access attribute belongs, when the property being accessed is a class property, instance is None;owner the class to which the descriptor instance is attached.
Test code:
classDescriptor:def __init__(self):Pass def __get__(self, instance, owner):Print(self, instance, owner)def __set__(self, instance, value):Print(self, instance, value)def __delete__(self, instance):Print(self, instance)classClassA (object): a=descriptor ()if __name__=='__main__': classa.a x=ClassA () x.a
As can be seen from the running result, when the Class property is accessed, instance is none; When you access an instance property, instance is the instance to which the property belongs.
When the instance property is None, the descriptor object directly accesses the class property of the class to which it is attached. When the instance property is not none, the descriptor accesses the instance properties directly.
<__main__. Descriptor object at 0x1081413c8> None <class'__main__. ClassA'><__main__. Descriptor object at 0x1081413c8> <__main__. ClassA object at 0x108141358> <class'__main__. ClassA'>
The third parameter of the __set__ method, value, is a property that needs to be assigned, and the two parameters of the __delete__ method are the same as the first two parameters of the __get__ method.
Python's properties and descriptors