import time
def outer(F):
def wrapper(x):
start = time.time()
F(x)
end = time.time()
print(end - start)
return wrapper
@outer
def func2(a):
if a == 1:
return 1
else:
return a * func2(a-1)
func2(7)
上面是一段python递归实现阶乘的代码,但是在加上装饰器之后就报错了,想请大家看下问题出在哪里?为什么调用的时候不用 @outer
这种方式,而用原始的 outer(func2)(7)
就没问题了?
报错如下:
[root@yang python]# python3 test.py
7.152557373046875e-07
Traceback (most recent call last):
File "test.py", line 22, in <module>
func2(7)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
File "test.py", line 6, in wrapper
F(x)
File "test.py", line 17, in func2
return a * func2(a-1)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
内层应该有返回值