The @ property annotator of Python is used to call a class's method into a class's property. then @ property creates another Annotator and assigns a value to the property using one method. After @ property is used in the class, set the read/write attributes of the class, read-only and write-only attributes. The all method sets the read/write attribute. you can set this attribute or read this attribute, as shown in row 28. if The _ init _ () method is not defined, this attribute can only be used if this attribute is set first. In row 32, if you want to know the value of the write attribute, an error is returned. In row 34, there is no way to continue calling the @ property modifier of readonly Python to convert a class method into a class property, then @ property creates another modifier and assigns a value to the attribute in one way. After @ property is used in the class, set the read/write attributes of the class, read-only and write-only attributes.
The all method sets the read/write attribute. you can set this attribute or read this attribute, as shown in row 28. if The _ init _ () method is not defined, this attribute can only be used if this attribute is set first. In row 32, if you want to know the value of the write attribute, an error is returned. In row 34, there is no way to continue assigning value to the readonly system attribute. After @ property is used here, Python "private variable" can be implemented. of course, it is not really private. The real private is complicated, but it can also be implemented through @ property? After that, I learned to write again.
class UseProperty(object): def __init__(self): self._all = 233 @property def all(self): return self._all @all.setter def all(self, v): self._all = v @property def readonly(self): return self._all @property def write(self): raise AttributeError('This is not a readonly attribute.') @write.setter def write(self, value): self._write = valuep = UseProperty()print p.allp.all = 100print p.allp.write = 233# print p.writeprint p.readonly# p.readonly = 10
The above is a detailed description of the basic usage of the property modifier in Python. For more information, see other related articles in the first PHP community!