4

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

    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()  
4

1 回答 1

8
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()

请参阅了解 Python 中的 kwargs教程。

于 2013-04-05T23:59:55.523 回答