Python Basic Learning 1-class Property access

Source: Internet
Author: User
#!/usr/bin/env python#-*-coding:utf-8-*-#====> __setattr__ overriding a magic method called when setting class object property values   __getattr__ (self,name) Get class properties # __getattribute__ (Self,name) is called before the property is obtained, __getattr__ is called First, __delattr__ (self,name) deletes the attribute when called Class Rectangle:    def __ Init__ (Self,wid,high): #重写类初始化方法        self.wid=wid        self.high=high    def __setattr__ (self, Key, value): # Override the method that sets the class property value, when the Set class any property calls this method        if key== "square": #如果给类属性square assignment sets aspect to the value set for the current square property            Self.wid=value            Self.high=value        Else:            super (). __setattr__ (Key,value) #否则调用父类的设置属性值魔法方法方法    def getarea (self):         return  SELF.HIGH*SELF.WIDR1 = Rectangle (4,5) print (R1.getarea ()) R1.square=10print (R1.high) # 10print (R1.WID) # 10print (R1.getarea ()) # 100

Python Basic Learning 1-class Property access

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.