Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型提示返回名称错误:名称';日期时间';未定义_Python_Python 3.x_Typing_Python Typing - Fatal编程技术网

Python 类型提示返回名称错误:名称';日期时间';未定义

Python 类型提示返回名称错误:名称';日期时间';未定义,python,python-3.x,typing,python-typing,Python,Python 3.x,Typing,Python Typing,我有下面这个函数 def time_in_range(start, end, x): """Return true if x is in the range [start, end]""" if start <= end: return start <= x <= end else: return start <= x or x <= end def time_在_范围内(开始、结束、x): “”“如果x在范

我有下面这个函数

def time_in_range(start, end, x):
    """Return true if x is in the range [start, end]"""
    if start <= end:
        return start <= x <= end
    else:
        return start <= x or x <= end
def time_在_范围内(开始、结束、x):
“”“如果x在范围[start,end]内,则返回true”“”

如果start您需要导入
datetime
,或者使用字符串(请记住,这只是一个提示)

def f(x:datetime): ... 通过 ... 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“datetime” >>>定义f(x:“日期时间”): ... 通过 ... >>> >>>从日期时间导入日期时间 >>>def f(x:日期时间): ... 通过 ... >>>

Python 3.7.4

要么
导入日期时间
并使用
日期时间.datetime
作为提示,要么
从日期时间导入日期时间
并使用
日期时间
作为提示

def time_in_range(start: datetime, end: datetime, x: datetime) -> bool:
    """Return true if x is in the range [start, end]"""
    if start <= end:
        return start <= x <= end
    else:
        return start <= x or x <= end