本文主要是介绍Python 学习入门(38)—— @functools模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.
functools 源码路径及内置函数:
利用@functools对函数运行时间,进行计时
代码示例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# blog.ithomer.netimport time, functoolsdef timeit(func):@functools.wraps(func)def __do__(*args, **kwargs):start = time.time()result = func(*args, **kwargs)print("%s usedtime: %ss" % (func.__name__, time.time() - start))return resultreturn __do__@timeit
def print_str(num):sum = 0for i in range(num):sum += iprint sum@timeit
def main():print("print_str(100)")print_str(100)print("print_str(10000)")print_str(10000)print("print_str(1000000)")print_str(1000000)if __name__ == "__main__": main()
运行结果:
print_str(100)
4950
print_str usedtime: 3.60012054443e-05s
print_str(10000)
49995000
print_str usedtime: 0.000550985336304s
print_str(1000000)
499999500000
print_str usedtime: 0.0614850521088s
main usedtime: 0.0623250007629s
说明:运行结果中的红色部分,都是运行计时的结果
示例2:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# blog.ithomer.netimport time, functoolsdef functools_wrapper(func):@functools.wraps(func)def wrapper(*args, **kwargs):print("call from functools_wrapper...")start = time.time()result = func(*args, **kwargs)print("%s usedtime: %ss" % (func.__name__, time.time() - start))
# return func(*args, **kwargs) return resultreturn wrapper@functools_wrapper
def functools_partial():print(int('10')) # 10print(int('10', 2)) # 2int2 = functools.partial(int, base=2)print(int2('10')) # 2print(int2('1010')) # 10int2 = functools.partial(int, base=8)print(int2('10')) # 8print(int2('1010')) # 520@functools_wrapper
def functools_reduce():array = [1, 2, 3, 4, 5, 6]result = reduce((lambda x,y:x*y), array)print("result = %d" % result) # 720result = functools.reduce((lambda x,y:x*y), array)print("result = %d" % result) # 720def main():functools_partial()functools_reduce()if __name__ == "__main__": main()
运行结果:
call from functools_wrapper...
10
2
2
10
8
520
functools_partial usedtime: 2.00271606445e-05s
call from functools_wrapper...
result = 720
result = 720
functools_reduce usedtime: 1.21593475342e-05s
参考推荐:
Python的functools模块
Python的functools
这篇关于Python 学习入门(38)—— @functools模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!