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