|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class Counter():
- def __init__(self):
- self.__dict__['counter'] = 0
- def __setattr__(self,name,value):
- super().__setattr__('counter',self.counter + 1)
- super().__setattr__(name,value)
- def __delattr__(self,name):
- ssuper().__setattr__('counter',self.counter -1)
- super().__delattr__(name)
复制代码
在init方法中定义了一个counter属性,为什么后面self.counter 就是init中的counter
- class Counte():
- def __init__(self):
- self.__dict__['counter'] = 0
- def a(self):
- print(counter)
复制代码
在我自己试验中实例化对象时,调用a方法会出错,如果a方法中的属性加了 self.counter就能正常运行。 这个要怎么理解这个变量呢 |
|