[蟒蛇菜谱]Python函数参数传递最佳实践

it2025-01-15  15

将函数作为参数传递,同时将该函数需要的参数一起传递。可参考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"])

转载于:https://www.cnblogs.com/tuzkee/p/3939620.html

相关资源:数据结构—成绩单生成器
最新回复(0)