2024-04-25 19:54:04 发布
网友
我想知道是否可以在运行时访问变量的类型注释,方法和函数可以使用inspect.getmembers()中的__annotations__条目。你知道吗
inspect.getmembers()
__annotations__
> a:Optional[str]=None > type(a) <class 'NoneType'> > a:str=None > type(a) <class 'NoneType'>
谢谢。你知道吗
locals()和globals()跟踪__annotations__键中变量的注释。你知道吗
locals()
globals()
>>> from typing import * >>> a: Optional[int] = None >>> locals()['__annotations__'] {'a': typing.Union[int, NoneType]} >>> locals()['__annotations__']['a'] typing.Union[int, NoneType] >>> >>> foo = 0 >>> bar: foo >>> locals()['__annotations__']['bar'] 0 >>> >>> baz: List[str] >>> locals()['__annotations__']['baz'] typing.List[str]
locals()
和globals()
跟踪__annotations__
键中变量的注释。你知道吗相关问题 更多 >
编程相关推荐