Python @property Properties

Source: Internet
Author: User

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

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.