|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在http://bbs.fishc.com/thread-56167-1-1.html这一讲的作业中,我对测试题2进行了修改,
- class C:
- def __getattr__(self, name):
- print(1)
- #return super().__getattr__(name)
- def __getattribute__(self, name):
- print(2)
- return super().__getattribute__(name)
- def __setattr__(self, name, value):
- print(3)
- #return super().__setattr__(name, value)
- def __delattr__(self, name):
- print(4)
- #return super().__delattr__(name)
复制代码
- >>> c=C()
- >>> c.x
- 2
- 1
- >>> c.x=1
- 3
- >>> c.x
- 2
- 1
- >>> print(c.x)
- 2
- 1
- None
复制代码
输出print(c.x),先访问__getattribute__,打印2,再就是打印c.x的值,也就是1,打印玩了怎么最后还打印了一个None???
因为你赋值操作的__setattr__没有写完整,并没有给c.x真正赋值,所以输出None。
把10行改为 super().__setattr__(name, value)
|
|