0

我想继承两个 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.Object并且dbus.service.Object有不同的元类,我也需要对它们进行子类化:

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.Object和的元类dbus.service.Object吗?

4

1 回答 1

1

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

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

于 2019-03-07T21:17:26.753 回答