如何将参数传递给datetime.timedelta

时间:2013-04-05 23:55:57

标签: python datetime python-3.x

我们已经知道,datetime.timedelta(hours = 5)是一个正确的语法。 如何传递一个可以替换下面函数中给出的'hours'的参数?

    def check_time(until_when, def_time, how_long):
            if until_when- datetime.datetime.now() > datetime.timedelta(def_time = how_long):
            input('Task has been finished.\nPress any key to quit\n')
            exit()  

1 个答案:

答案 0 :(得分:5)

def check_time(until_when, def_time, how_long):
    arg_dict = {def_time:how_long}
    dt = datetime.timedelta(**arg_dict)
    if until_when- datetime.datetime.now() > dt:
        input('Task has been finished.\nPress any key to quit\n')
        exit()

请参阅Understanding kwargs in Pythonthe tutorial.