gpt4 book ai didi

python - Python 的内置 __build_class__ 有什么作用?

转载 作者:IT老高 更新时间:2023-10-28 20:47:42 34 4
gpt4 key购买 nike

在 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) 函数有什么区别?

最佳答案

编译PEP 3115元类

Guido van Rossum said:

The PEP proposes that the class statement accepts keyword arguments, *args, and **kwds syntax as well as positional bases. This is a bit messy to compile and execute, but we already have this, of course, in the code for calling regular functions.

So I think it would be acceptable to this into a call to a new (hidden) built-in function, named __build_class__. Then that this class definition:

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

would translate into this:

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

where <func> is a function object for the class body.

关于python - Python 的内置 __build_class__ 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1832997/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com