这个问题在这里已经有了答案:
Python Typing: Given Set of Values
(2 个回答)
去年关闭。
我有一个可以采用固定值列表的函数:例如
def func(mode="a"):
if mode not in ["a", "b"]:
raise AttributeError("not ok")
有没有办法输入提示它只能是这两个值之一?
最佳答案
我想你想要一个 literal type :
def func(mode="a": Literal["a", "b"]):
if mode not in ["a", "b"]:
raise AttributeError("not ok")
这是在 Python 3.8 中引入的,通过 PEP 586 .
关于python - 输入可能值列表的提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874373/