Python函数式编程
定义:对别的函数进行封装,运算,操作的函数
参数:
支持传别的函数名
任意参数 *args **kwargs
#coding:gbk def test(m,n): return m+n def func(func_name,*args,**kwargs): ret = func_name(*args,**kwargs) return ret+1 print(func(test,8,9))
超时处理函数
#coding:gbk import time
import os FILE_PATH = r'C:\Users\Martin\Desktop\finish' def check_file_exist(file_name): return os.path.exists(file_name) def handle_timeout(func_name, timeout, *args, **kwargs): interval = 1 ret = None while timeout > 0: begin_time = time.time() ret = func_name(*args, **kwargs) if ret: break time.sleep(interval) timeout -= time.time() - begin_time return ret ret = handle_timeout(check_file_exist,15,FILE_PATH) print(ret)
import time FILE_PATH = r'C:\Users\Martin\Desktop\finish' def install(): time.sleep(20) f = open(FILE_PATH,'w') f.close() install()