课后作业45
class Counter:k = []
def __init__(self):
self.counter = 0
def __setattr__(self, name, value):
if name != 'counter': 问下为什么一定要加这步 把counter换成其他的就会报错
if name not in self.k:
self.counter += 1
self.k.append(name)
super().__setattr__(name, value)
def __delattr__(self, name):
self.counter -= 1
self.k.remove(name)
super().__delattr__(name)
因为第一步 def __init__(self):
self.counter = 0
就会调用__setattr__(self, name, value):
也就是
if name == 'counter':
super().__setattr__(name, value)
页:
[1]