马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Tip:
一,理论
二,应用
1
class C:
def __getattribute__(self, name):
print('getattribute')
return super().__getattribute__(name)
def __getattr__(self, name):
print('getattr')
def __setattr__(self, name, value):
print('setattr')
super().__setattr__(name, value)
def __delattr__(self, name):
print('delattr')
super().__delattr__(name)
>>> c = C()
>>> c.x
getattribute #优先调用
getattr
>>> c.x = 1
setattr
>>> del c.x
delattr
2class Rectangle:
def __init__(self, width = 0, height = 0):
self.width = width
self.height = height
def __setattr__(self, name, value):
if name == 'square':
self.width = value
self.height = value
else:
# super().__setattr__(name, value)
super().__dic__[name] = value
def cal(self):
return self.width * self.height
三,课后练习 |