带字符串的Typehint列表和mypy中的字符串列表



一个类型如何提示以下内容:

def f(x : <type>) -> None: print(x)

其中,x是包含字符串的列表,以及字符串列表。

例如:

# example 1
['a', 'sdf', ['sd', 'sdg'], 'oidsf d', 'as', ['asf','asr']]
# example 2
[['as', 'asfd'], ['oei', 'afgdf'], 'egt', 'aergaer']

您想要的类型是Union:

from typing import List, Union

def f(x: List[Union[str, List[str]]]) -> None:
print(x)

f(['a', 'sdf', ['sd', 'sdg'], 'oidsf d', 'as', ['asf', 'asr']])
f([['as', 'asfd'], ['oei', 'afgdf'], 'egt', 'aergaer'])

CCD_ 3是";可以是字符串或字符串列表的项目列表";。

最新更新