Use of property in Python

Source: Internet
Author: User

Remember that there is also the concept of property in cocoa, the property in Python and the property in cocoa seems to be similar. Here are two ways to use it.

Usage One:

classTest (object):def __init__(self):Print "Init is calling"SELF.A=100defGetX (self):Print "Call GetX"        returnSELF.AdefSetX (self,value):Print "Call SetX"SELF.A=value x=Property (GETX,SETX)defMain (): a=Test ()PrintA.x#100a.x=150PrintA.x# Maxif __name__=='__main__': Main ()

Getx and Setx These two function names can be arbitrarily taken. There's also a Del method, which is less commonly used, so it's not written here.

Usage Two:

classTest (object):def __init__(self):Print "Init is calling"SELF.A=100@propertydefx (self):Print "Call GetX"        returnSELF.A @x.setterdefx (self,value):Print "Call SetX"SELF.A=valuedefMain (): T=Test ()PrintT.x# -t.x=150PrintT.x# Maxif __name__=='__main__': Main ()

Notice that the function name is changed to X.

Use of property in Python

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.