PyGObject、dbus python 和元类

PyGObject, dbus python and metaclasses

我想子类化两个 python 类:一个来自 PyGObject,一个来自 python3-dbus:

import gi
from gi.repository import GObject
import dbus.service

class Test(GObject.Object, dbus.service.Object):
    pass

但是我收到以下错误:

$ python3 test.py
Traceback (most recent call last):
    File ".../test.py", line 5, in <module>
       class Test(GObject.Object, dbus.service.Object):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

我已经发现这是因为GObject.Objectdbus.service.Object有不同的meta类,我也需要对它们进行子类化:

class M_Test(gi.types.GObjectMeta, dbus.service.InterfaceType):
    pass

class Test(GObject.Object, dbus.service.Object):
    __metaclass__=M_Test
    pass

然而这并没有帮助,我继续收到同样的错误。也许 gi.types.GObjectMetadbus.service.InterfaceType 不是 GObject.Objectdbus.service.Object 的正确元 类。有人知道如何合并 GObject.Objectdbus.service.Object 的元 类 吗?

Python3 更改了指定元类的语法。 PEP-3115

现在 class Test(GObject.Object, dbus.service.Object, metaclass=M_Test):