gpt4 book ai didi

python - 在 python 中使用元类创建类的新实例

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:06 25 4
gpt4 key购买 nike

我想在 python 中动态创建一个具有特定元类的类的新实例。

我已经试过了:

type('A', (object,  metaclass=tmp, ), {})

但不起作用:(

有人知道我怎样才能做到这一点吗?

最佳答案

元类通常是 type 的子类(或者可能很少是将 name, bases, clsdict 作为参数的函数),因此您应该能够使用它像类型。如果 tmp 是您的元类,则尝试:

tmp('A',(object,),{})

关于python - 在 python 中使用元类创建类的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5780876/

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