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 源码路径及内置函数:

Python 学习入门(38)—— functools模块_第1张图片


利用@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 result    return __do__@timeitdef print_str(num):    sum = 0    for i in range(num):        sum += i    print sum@timeitdef 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 result    return wrapper    @functools_wrapperdef functools_partial():    print(int('10'))        # 10    print(int('10', 2))     # 2    int2 = functools.partial(int, base=2)    print(int2('10'))       # 2    print(int2('1010'))     # 10    int2 = functools.partial(int, base=8)    print(int2('10'))       # 8    print(int2('1010'))     # 520   @functools_wrapperdef functools_reduce():    array = [1, 2, 3, 4, 5, 6]    result = reduce((lambda x,y:x*y), array)    print("result = %d" % result)           # 720        result = 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


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

你可能感兴趣的:(Python 学习入门(38)—— functools模块)