[蟒蛇菜谱]Python函数参数传递最佳实践
将函数作为参数传递,同时将该函数需要的参数一起传递。可参考threading.Timer的处理方式:
class threading.Timer(interval, function, args=[], kwargs={})
Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.
内部实现中,调用function的方式:
初始化:
def __init__(self, interval, function, args=[], kwargs={}):
Thread.__init__(self)
self.function = function
self.args = args
self.kwargs = kwargs
调用:
self.function(*self.args, **self.kwargs)
使用方式:
t = Timer(1, foo, args=["hello"])