Introduction to the basic usage of the property modifier in Python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.