#!/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