Смысл примера-показать как обарачивать методы классов.
decorators.py:
class tracer(object): def __init__(self, func): self.calls=0 self.func=func def __call__(self, *args, **kwargs): self.calls+=1 print 'call %s to %s' % (self.calls, self.func.__name__) self.func(*args, **kwargs) def __get__(self, instance, owner): def wraper(*args, **kwargs): print self print instance print owner print instance.name return self(instance, *args, **kwargs) return wraper class Person(): def __init__(self, name, pay): self.name = name self.pay = pay @tracer def giveRaise(self, percent): self.pay *= 1.0 + percent @tracer def lastName(self): return self.name.split()[-1] if __name__=='__main__': bob = Person('Bob Smith', 50000) sue = Person('Sue Jones', 100000) print bob.lastName() print sue.lastName()
Вот результат работы модуля:
<__main__.tracer object at 0x00000000021CFCC0> <__main__.Person instance at 0x00000000021D2A88> __main__.Person Bob Smith call 1 to lastName None #вот тут пытаюсь проследить чего не так пошло <__main__.tracer object at 0x00000000021CFCC0> <__main__.Person instance at 0x00000000021D2AC8> __main__.Person Sue Jones call 2 to lastName None #и тут соответственно
подскажите где алгоритм некорректно работает??
Всё по книжечке…