#! python
def logger(func):
def inner(*args, **kwargs):
print("Arguments are : %s, %s" % args, kwargs)
return func(*args, **kwargs)
return inner
@logger
def foo1(x, y=1):
return x*y
@logger
def foo2():
return 2
foo1(5, 4)
foo1(3, y="alex")
foo2()
出错是:
Arguments are : 5, 4 {}
Traceback (most recent call last):
File ".\decorator.py", line 18, in
foo1(3, y="alex")
File ".\decorator.py", line 5, in inner
print("Arguments are : %s, %s" % args, kwargs)
TypeError: not enough arguments for format string
我用的python是3.4.0
这个其实跟 python 3 和 decorator 关系都不太大,是因为
print
用法有些错误,应该写作如下形式,注意括号。