Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以在运行时获取typing.List的类型参数?_Python_Python 3.x_Python 3.5 - Fatal编程技术网

Python 是否可以在运行时获取typing.List的类型参数?

Python 是否可以在运行时获取typing.List的类型参数?,python,python-3.x,python-3.5,Python,Python 3.x,Python 3.5,假设我有一个typing.List[str]对象,是否可以在运行时从类型提示对象获取类型参数?我想要一个基本上返回str类的字段或方法。我做了一些挖掘,虽然没有文档记录或官方的,但使用\uuu args\uu是有效的 >>> Union[str, bool].__args__ (<class 'str'>, <class 'bool'>) >>> List[str].__args__ (<class 'str'>,) >>

假设我有一个
typing.List[str]
对象,是否可以在运行时从类型提示对象获取类型参数?我想要一个基本上返回
str
类的字段或方法。

我做了一些挖掘,虽然没有文档记录或官方的,但使用
\uuu args\uu
是有效的

>>> Union[str, bool].__args__
(<class 'str'>, <class 'bool'>)
>>> List[str].__args__
(<class 'str'>,)
>>联合[str,bool]。\uu参数__
(, )
>>>列表[str]。\uuu参数__
(,)
出于某种原因,int和bool的并集只是一个int,我不敢在这个模块的混乱中进一步挖掘

>>> Union[int, bool]
<class 'int'>
>>> Union[bool, int]
<class 'int'>
联合[int,bool] >>>联合[bool,int]
是的,但不是以一年后被记录或保证存在的方式。现在运行时使用
键入的情况非常糟糕。
Union[int,bool]
int
,因为
bool
int
的一个子类(请参见
issubclass(bool,int)
help(bool)