提问在注释里
def object_counter(class_):
class_.__getattr__orig = class_.__getattribute__#class_.__getattribute__是一个method, 这里是赋值给变量的意思?
def new_getattr(self, name):
if name == 'mileage':
print('We noticed that the mileage attribute was read')
return class_.__getattr__orig(self, name)#能否解释一下这个return的意义
class_.__getattribute__ = new_getattr#这里也看不懂
return class_#这里的class_到底算变量,还是类
@object_counter
class Car:
def __init__(self, VIN):
self.mileage = 0
self.VIN = VIN
car = Car('ABC123')
print('The mileage is', car.mileage)
print('The VIN is', car.VIN)