# определение декораторов
# @A @B @C
# def f()
# эквивалент при вызове
# x = A(B(C(f())))
# Класс-декоратор
class trace:
def __init__(self, func):
self.calls = 0
self.func = func
def __call__(self,*args):
self.calls+=1
print "call %s to %s" % (self.calls, self.func.__name__)
self.func(*args)
@trace
def test_function(a,b,c):
print a,b,c
test_function(1,2,3)
Python Справочник v0.05 © 2007-2024 Igor Salnikov aka SunDoctor