为啥那个print factorial(10)在py3上运行一直报错
为啥那个print factorial(10)在py3上运行一直报错
为啥那个print factorial(10)在py3上运行一直报错
2017-11-20
#python3 中print和python2中的用法不同,python3是print(),必须加括号。
#python3中map(),filter(),reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里,需要先引用:from functools import reduce
import time
from functools import reduce
def performance(unit):
def interval_func_run(f):
def fn(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
t = (t2 - t1) * 1000 if unit=='ms' else (t2 - t1)
print('call %s() in %fs' % (f.__name__, (t)))
return r
return fn
return interval_func_run
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print(factorial(10))
举报