Python的内置__build_class__有什么作用?

发布于 2021-01-29 17:00:43

在Python 3.1中,模块中有一个我不知道的新内置函数builtins

__build_class__(...)
    __build_class__(func, name, *bases, metaclass=None, **kwds) -> class

    Internal helper function used by the class statement.

此功能有什么作用?如果它是内部的,为什么必须在内置文件中?功能有什么区别type(name, bases, dict)

关注者
0
被浏览
189
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    编译 PEP 3115元类

    吉多·范·罗苏姆(Guido vanRossum)说:

    PEP建议class语句接受关键字参数
    *args,和**kwds语法以及位置基础。编译和执行这有点麻烦,但是我们在调用常规函数的代码中当然已经有了它。

    因此,我认为调用新的(隐藏)内置函数名为可以接受 __build_class__。然后,这个类定义:

      class C(A, B, metaclass=M, other=42, *more_bases, *more_kwds):
        ...
    

    会翻译成这样:

      C = __build_class__(<func>, 'C', A, B, metaclass=M, other=42,
    *more_bases, *more_kwds)
    

    <func>类主体的功能对象在哪里。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看