访问输入。文字值

问题描述 投票:0回答:1

如何访问(并迭代)

typing.Literal
的文字?

对于上下文来说,这是为了避免两次定义文字列表。

python python-typing
1个回答
3
投票

typing.get_args
可以用于此目的。

MyLiteral = Literal["foo", "bar"]

for literal_option in typing.get_args(MyLiteral):
    print(literal_option)
© www.soinside.com 2019 - 2024. All rights reserved.