查找Python中重新定义之前使用的__metaclass__
我想重新定义 __metaclass__ 但我想退回到如果我没有重新定义就会使用的元类。
class ComponentMetaClass(type):
def __new__(cls, name, bases, dct):
return <insert_prev_here>.__new__(cls, name, bases, dct)
class Component(OtherObjects):
__metaclass__ = ComponentMetaClass
据我了解,默认使用的 __metaclass__ 会经历检查类范围内的定义、然后在基类中、然后在全局中检查定义的过程。通常,您会在重新定义中使用类型,并且通常是全局类型,但是,我的其他对象可能重新定义了 __metaclass__。所以在使用类型时,我会忽略它们的定义,并且它们不会运行,对吗?
编辑:请注意,直到运行时我才知道其他对象是什么
I want to redefine a __metaclass__ but I want to fall back to the metaclass which would have been used if I hadn't redefined.
class ComponentMetaClass(type):
def __new__(cls, name, bases, dct):
return <insert_prev_here>.__new__(cls, name, bases, dct)
class Component(OtherObjects):
__metaclass__ = ComponentMetaClass
From what I understand, the __metaclass__ used by default goes through the process of checking for a definition in the scope of the class, then in the bases and then in global. Normally you would use type in the redefinition and that is usually the global one, however, my OtherObjects, may have redefined the __metaclass__. So in using type, I would ignore their definition and they wouldn't run, right?
edit: note that I don't know what OtherObjects are until runtime
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @unutbu 所说:“在一个类层次结构中,元类必须是彼此的子类。也就是说,Component 的元类必须是 OtherObjects 元类的子类。”
这意味着您的问题比您首先要复杂一些 - 不仅您必须从基类调用正确的元类,而且您当前的元类也必须从基类正确继承。
(破解一些代码,面对奇怪的行为,90 分钟后回来)
这确实很棘手 - 我必须创建一个类,该类接收所需的元类作为参数,并且 __call__ 方法动态生成一个新的元类,修改其基类并向其添加 __superclass 属性。
但这应该可以满足您的需求,甚至更多 - 您只需从 BaseComponableMeta 继承所有元类,并通过元类“__superclass”属性调用层次结构中的超类:
As @unutbu puts it: "Within one class hierarchy, metaclasses must be subclasses of each other. That is, the metaclass of Component must be a subclass of the metaclass of OtherObjects."
Which means your problem is a bit more complicated than you though first - Not only you have to call the proper metaclass from the base classes, but your current metaclass has to properly inherit from then as well.
(hack some code, confront strange behavior, come back 90 min later)
It was tricky indeed - I had to create a class that receives the desired metaclass as a parameter, and which __call__ method generates dynamically a new metaclass, modifying its bases and adding a __superclass attribute to it.
But this should do what you want and some more - you just have to inherit all your metaclasses from
BaseComponableMeta
and call the superclasses in the hyerarchy through the metaclass "__superclass" attribute:在一个类层次结构中,元类必须是彼此的子类。也就是说,
Component
的元类必须是OtherObjects
元类的子类。如果您没有为
Component
命名__metaclass__
,则默认情况下将使用OtherObjects
的元类。如果
ComponentMetaClass
和OtherObjectsMeta
都继承(独立)自type
:那么您会收到此错误:
但是如果您创建
ComponentMetaClass
> 作为OtherObjectsMeta
的子类,然后错误就会消失。
也许我误读了你的问题。如果想要
ComponentMetaClass.__new__
调用OtherObjectsMeta.__new__
,则使用super
:关于使用元类的替代方法,在注释中提到。使用超级:
Within one class hierarchy, metaclasses must be subclasses of each other. That is, the metaclass of
Component
must be a subclass of the metaclass ofOtherObjects
.If you don't name a
__metaclass__
forComponent
then the metaclass ofOtherObjects
will be used by default.If
ComponentMetaClass
andOtherObjectsMeta
both inherit (independently) fromtype
:then you get this error:
but if you make
ComponentMetaClass
as subclass ofOtherObjectsMeta
then the error goes away.
Perhaps I misread your question. If want
ComponentMetaClass.__new__
to callOtherObjectsMeta.__new__
, then usesuper
:Regarding an alternative to using metaclasses, mentioned in the comments. Use
super
: