Python @property Modifiers
The property () function of Python, which is a function of the built-in function, returns the properties of the attribute: You can view its description on the following Web page:
The document says property () as a modifier, which creates a read-only attribute.
class Parrot: def __init__ (self): = 100000 @property def voltage (self): "" "Get the Current voltage. """ return Self._voltage
In this case, the @property modifier transforms the voltage () method into a read-only property "getter", which also sets the docstring of voltage to "Get the current voltage"
classC:def __init__(self): self._x=None @propertydefX (self):"""I ' m the ' X ' property.""" returnself._x @x.setterdefX (self, value): Self._x=value @x.deleterdefX (self):delSELF._XC=C () c.x= 2Print(c.x)Print(c._x)#2#2
This code can also be written using a non-modifier method, see below
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.") C=C () c.x= 2Print(c.x)Print(c._x)#2#2
The meaning of these two pieces of code, is to set a property () to manage the properties of the Self._x function, the future management of _x, can be through the object X.
Python @property Properties