-
- The following comes from Python 3.6.0 Document:
-
-
-
-
class
property (
fget=none,
fset=none,
fdel=none,
doc=none)
-
Return a property attribute.
Fget is a function for getting an attribute value. Fset is a function for setting an attribute value. Fdel is a function for deleting an attribute value. and Doc creates a docstring for the attribute.
A typical use are to define a managed attribute x :
classC:def __init__(self): self._x=NonedefGetx (self):returnself._xdefsetx (self, value): Self._x=valuedefDelx (self):delself._x x= Property (Getx, Setx, Delx,"I ' m the ' X ' property.")
If C is an instance of C, would c.x invoke the getter, would invoke the setter and the c.x = value del c.x delet Er.
If given, Doc 'll is the docstring of the property attribute. Otherwise, the property would copy Fget' s docstring (if it exists). This makes it possible to create read-only properties easily using as property() a decorator:
class Parrot: def __init__ (self): = 100000 @property def voltage (self): "" "Get the Current voltage. """ return Self._voltage
The @property decorator turns the voltage() method into a ' getter ' for a read-only attribute with the same name, and it sets the D Ocstring for voltage to "Get the current voltage."
A Property object have getter , setter and deleter methods usable as decorators that create a copy of the property with the Cor Responding accessor function set to the decorated function. This is the best explained with an example:
class C: def __init__ = None @property def X (self): i ' m the ' X ' property. return self._x @x.setter def X (self, value): self._x = value @x.deleter def X (self): del self._x
This code was exactly equivalent to the first example. Be sure to give the additional functions the same name as the "original property x "
The returned Property object also have the attributes fget , fset and corresponding to the fdel constructor arguments.
Changed in version 3.5:the Docstrings's Property objects is now writeable.
Look at the code first:
classStu:def __init__(self,name): Self.name=name defGet_score (self):returnSelf.scoredefSet_score (self, score):if notisinstance (score, int):RaiseValueError ("score must is an integer~") ifscore>100orscore<0:RaiseValueError ("score must between 0~100") Self.score=scores= Stu ("Botoo")#S.set_score (#score must) is an integer~#S.set_score ("DASDA") #score must between 0~100S.set_score (98)Print(S.get_score ())#98
This use of the Get/set method to encapsulate access to a property is common in many object-oriented programming languages.
But writing S.get_score () and S.set_score () did not directly write S.score directly.
Because Python supports higher-order functions, you can use adorner functions to "decorate" the Get/set method into a property invocation:
Further comparisons:
classStudent:def __init__(self,name): Self.name=name @propertydefscore (self):returnself._score @score. Setterdefscore (self,value):if notisinstance (value, int):RaiseValueError ('The score must be an integer.') ifValue < 0orValue > 100: RaiseValueError ('score must be between 0-100') Self._score=value S1= Student ("Botoo") S1.score= 50Print(S1.score)# -S1.score = 500#ValueError: Score must be between 0-100
The first score (self) is the Get method, decorated with @property, the second score (self, score) is the set method, decorated with @score.setter, @score. Setter is the previous @ The property is decorated by-product.
Extending a property in a subclass can cause a lot of subtle problems, because a property is actually a collection of getter , setter and deleter methods, rather than a single method. So, when you extend a property, you need to decide whether you want to redefine all the methods or just modify one of them.
Reference:
Http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p08_extending_property_in_subclass.html
77804137
Python Basics = = = Adorner @property