[Python learning] Describes the property of Python, and why to use a setter, a little chestnut

Source: Internet
Author: User

The property in Python is more useful.

First, a code.

#-*-coding:utf-8-*-class C (object): Status_dict = {1: ' Accept ', 2: ' Reject '}def __init__ (self): self._x = 1@propertydef St ATUs (self): return self.status_dict[self._x] @status. Setterdef status (Self, val): if val = = ' Reject ': self._x = 2elif val = = ' Accept ': self._x = 1c = C () print c.statusprint c._xc.status = ' reject ' Print c.statusprint c._x

This code with the property decorator also used the setter, I used to use more property, such as I have developed a state, but want to get this state corresponding to the Chinese description, plus a properties, so you can use this state as the object's attributes to operate, But I know the description of the state want to change the description, what to do, and I do not know the corresponding relationship, this time the setter to play.

-------------------

Cut first, write so much first, and then slowly Add.

[Python learning] Describes the property of Python, and why to use a setter, a little chestnut

Related Article

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.