Chr*_*uer 14 python type-hinting
我有一个可以采用固定值列表的函数:例如
def func(mode="a"):
if mode not in ["a", "b"]:
raise AttributeError("not ok")
Run Code Online (Sandbox Code Playgroud)
有没有办法输入提示它只能是这两个值之一?
jon*_*rpe 27
我认为你想要一个文字类型:
def func(mode = "a": Literal["a", "b"]):
if mode not in ["a", "b"]:
raise AttributeError("not ok")
Run Code Online (Sandbox Code Playgroud)
这是通过PEP 586在 Python 3.8 中引入的。
| 归档时间: |
|
| 查看次数: |
10883 次 |
| 最近记录: |