Python 为代码对象输入 hint/annotation
Python type hint/annotation for code objects
是否有代码对象的 Python 类型 hint/annotation(由 compile
在源代码字符串上返回,或调用 __code__
属性的结果方法)?我有一个接受单个代码对象参数的方法,我想适当地使用 typing
库对其进行适当的注释。
>>> c = compile('x = 1', 'test', 'single')
>>> <code object <module> at 0x1075f8660, file "test", line 1>
>>> c
>>> code
>>> type(c)
>>> type
>>> typing.get_type_hints(c)
>>> {}
是的,它在模块 types
:
中可用
from types import CodeType
code: CodeType = compile('x = 1', 'test', 'single')
是否有代码对象的 Python 类型 hint/annotation(由 compile
在源代码字符串上返回,或调用 __code__
属性的结果方法)?我有一个接受单个代码对象参数的方法,我想适当地使用 typing
库对其进行适当的注释。
>>> c = compile('x = 1', 'test', 'single')
>>> <code object <module> at 0x1075f8660, file "test", line 1>
>>> c
>>> code
>>> type(c)
>>> type
>>> typing.get_type_hints(c)
>>> {}
是的,它在模块 types
:
from types import CodeType
code: CodeType = compile('x = 1', 'test', 'single')