__咖宁123__ 2023-02-04 18:40 采纳率: 80%
浏览 50
已结题

Python如何引用内置类function

我在编辑代码时需要使用Python的类function,但是将跟类function的代码加入后却出现如下错误:

Traceback (most recent call last):
  File "x x x.py", line 58, in <module>
      ............................
       ^^^^^^^^
NameError: name 'function' is not defined

使用VSCode也可以在builtins.pyi找到该类:

class function:
    # Make sure this class definition stays roughly in line with `types.FunctionType`
    @property
    def __closure__(self) -> tuple[_Cell, ...] | None: ...
    __code__: CodeType
    __defaults__: tuple[Any, ...] | None
    __dict__: dict[str, Any]
    @property
    def __globals__(self) -> dict[str, Any]: ...
    __name__: str
    __qualname__: str
    __annotations__: dict[str, Any]
    __kwdefaults__: dict[str, Any]
    if sys.version_info >= (3, 10):
        @property
        def __builtins__(self) -> dict[str, Any]: ...

    __module__: str
    # mypy uses `builtins.function.__get__` to represent methods, properties, and getset_descriptors so we type the return as Any.
    def __get__(self, obj: object | None, type: type | None = ...) -> Any: ...

即使加入“from builtins import function”,却仍然抛出相同错误。
请问如何才能引用这个叫“function”的类?

  • 写回答

3条回答 默认 最新

  • P2441M 2023-02-04 21:05
    关注

    说直接一点,function这个类虽然在builtins里有定义,但是是内建类型,不能访问。你看看vscode里这个注释:

    img

    里面写了,Doesn't exist at runtime,运行时不存在。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月5日
  • 已采纳回答 2月5日
  • 创建了问题 2月4日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号