我有一個代碼裝飾和錯誤()函數的參數1必須是代碼,而不是str的
def timer_dec(f):
def wrapper(*args, **kwargs):
t = time.time()
args[0].debug('<{}> start'.format(f.__name__))
res = f(*args, **kwargs)
args[0].debug('<{}> finish'.format(f.__name__))
args[0].debug("Working time for function <%s>: %f" % (f.__name__, time.time() - t))
return res
return wrapper
這是正常工作:
@timer_dec
class A(object):
pass
但是,這是行不通的:
@timer_dec
class A(object):
pass
class B(A):
pass
TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str
Python version is 2.7
你能舉一個裝飾器的例子嗎? – ngelik 2013-03-01 10:38:13