马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
测试题:
0.
意图是让实例对象在赋值的时候值加1.
不对.
应该改为:
super().__setattr__(name,value + 1)
1.
__getattribute__(self,name)
2.
3
2 和None
3.
2
1
>>> c = C()
>>> c.x
2
1
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
c.x
File "<pyshell#29>", line 4, in __getattr__
return super().__getattr__(name)
AttributeError: 'super' object has no attribute '__getattr__'
super,没有getattr
4.
self.counter 会一直加1
self.counter = 1,这时候等式左边调用__getattr__方法,里面又有一个self.counter += 1,而这时候self.counter没有值,所以会出错.
动动手:
0.
class A:
def __getattr__(self,name):
return '该属性不存在!'
1.
class Demo:
def __init__(self,x = 'FishC'):
self.x = x
2.
母鸡
class Counter:
def __init__(self):
super().__setattr__('counter', 0)
def __setattr__(self, name, value):
super().__setattr__('counter', self.counter + 1)
super().__setattr__(name, value)
def __delattr__(self, name):
super().__setattr__('counter', self.counter - 1)
super().__delattr__(name)
|