Advanced features of Python 10: Boring @property

Source: Internet
Author: User

@property adorners are actually a bit boring, it's not necessary to take them out alone as a knowledge point, although it can turn the method into a property that makes the get and set methods work better, but it destroys the simplicity of Python (not the simplicity of the code but the syntax).

Here's why I'm saying this.

First, look at a use property.

class Student (object):    @property    def  testname (self):        return  Self.name    @testname. Setter    def  testname (self,name):        ==   "Alex"print(s.testname)

The result is:

/system/library/frameworks/python.framework/versions/3.4/bin/python3/users/lsf/pycharmprojects/py3project/  Test3.pyalexprocess finished with exit code 0

It does look good, but in fact Python's built-in __getattr__ and __setattr__ are ways to turn the method into a property feature.

 class   Stu (object):  def   pass  def  __setattr__   (Self,name,value): Self.  __dict__  [Name] = __getattr__   return  Span style= "color: #000000;" > Self.names  = Stu () s.name  = "  Span style= "color: #800000;" >alex   print  (s.name) 

The result is of course:

/system/library/frameworks/python.framework/versions/3.4/bin/python3/users/lsf/pycharmprojects/py3project/  Test3.pyalexprocess finished with exit code 0

Advanced features of Python 10: Boring @property

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.