小编Vla*_*tin的帖子

字符串选项的 Python 3 类型提示

比方说,我有一个函数,它有一个与方法名称对应的字符串参数:

def func(method: str):
    if method not in ('simple_method', 'some_other_method'):
        raise ValueError('Unknown method')
Run Code Online (Sandbox Code Playgroud)

我可以添加所有可能支持的选项(字符串)作为此参数的类型提示吗?例如,像这样的东西(因为打字时没有 Str,所以不起作用):

from typing import Str

def func(method: Str['simple_method', 'some_other_method']):
    ...
Run Code Online (Sandbox Code Playgroud)

python type-hinting

10
推荐指数
1
解决办法
5024
查看次数

标签 统计

python ×1

type-hinting ×1