简体   繁体   English

如何使用内置 function 的 `type()` 传递 class 关键字 arguments?

[英]How to pass class keyword arguments with the `type()` built-in function?

The class statement takes keyword arguments for various features. class语句将关键字 arguments 用于各种功能。 For example, the __init_subclass__ function receives keyword arguments passed at the class declaration:例如, __init_subclass__ function 接收在 class 声明中传递的关键字 arguments:

class A(object):
  def __init_subclass__(cls, **kwargs):
    print(f'{kwargs=}')

class B(A, my_arg=5):
  pass
kwargs={'my_arg': 5}

However, doing so prevents dynamic class creation: The type function does not seem to take the class keyword arguments.但是,这样做会阻止动态 class 创建: type function 似乎没有采用class关键字 ZDBC11CAABD5BDA99F77E6FB4E。 A factory function does , but may conflict with __init_subclass__ or similar when the factory must modify the class. 工厂 function 可以,但是当工厂必须修改 class 时,可能与__init_subclass__或类似内容冲突。

I would like to use the type() built-in function in its 3-arguments version to dynamically create a class.我想在其 3 参数版本中使用type()内置 function 来动态创建 class。 However, there seems to be no documented way to pass keyword arguments (like my_arg in the previous example).但是,似乎没有记录传递关键字 arguments 的方法(如前面示例中的my_arg )。

What would be the recommended way of doing it?推荐的方法是什么?

The bare type is not the equivalent of class statements in Python3.type不等同于 Python3 中的class语句。 Use types.new_class as a functional equivalent of class statements.使用types.new_class作为class语句的功能等效项。

class B(A, my_arg=5):
  a = 12  # non-trivial class body

B = types.new_class(
    'B',
    # base classes of `class` statement
    bases=(A,),
    # keywords to the `class` statement
    kwds={'my_arg': 5},
    # the class body as a function
    exec_body=lambda body: body.update({'a': 12}),
)

Keep in mind that a class statement can always be wrapped in a function to allow a parameterised, functional class creation.请记住,始终可以将class语句包装在 function 中,以允许创建参数化的功能 class。 This also allows to set most special attributes, eg the class name via __qualname__ :这也允许通过__qualname__设置大多数特殊属性,例如 class 名称:

def make_b(my_arg, my_a, name=None):
    class Parameterized_B(A, my_arg=my_arg):
        a = my_a
        if name is not None:
            __qualname__ = name
    return Parameterized_B

B = make_b(5, 12, "B")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM