比较先前定义的时间

时间:2017-08-03 08:20:46

标签: python

我是Python的新手。

我想使用 prior_defined_time 变量,将当前日期时间值与上次执行此函数时的值进行比较。怎么做?

import datetime

if foo():
    now = datetime.datetime.now()
    if (now – priviously_defined_time) > 1000:
        ..
        some code
        ..
        priviously_defined_time = datetime.datetime.now()

1 个答案:

答案 0 :(得分:0)

使用关键字global

import datetime
#define somewhere your  previously_defined_time 
previously_defined_time = 0

if foo():
    # use the word global to indicate that previously_defined_time is a global variable
    global previously_defined_time
    now = datetime.datetime.now()
    if (now – previously_defined_time) > 1000:
        # ..
        # some code
        # ..
        previously_defined_time = datetime.datetime.now()