【问题标题】:Python type hint/annotation for code objects代码对象的 Python 类型提示/注释
【发布时间】:2020-06-06 21:31:45
【问题描述】:

是否有代码对象的 Python 类型提示/注释(由源代码字符串上的 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)
>>> {}

【问题讨论】:

    标签: python python-3.x types type-hinting python-typing


    【解决方案1】:

    是的,它在模块types中可用:

    from types import CodeType
    code: CodeType = compile('x = 1', 'test', 'single')
    

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 2017-11-20
      • 2023-03-31
      • 2016-04-03
      • 2021-11-27
      • 2012-07-28
      • 2015-02-17
      相关资源
      最近更新 更多