比较python中两个类实例的内容的最有效方法

Kla*_*kus 1 python object instance

我正在寻找比较两个类实例的内容的最有效方法.我有一个包含这些类实例的列表,在附加到列表之前,我想确定它们的属性值是否相同.这对大多数人来说似乎微不足道,但在仔细阅读这些论坛后,我无法具体到我想要做的事情.另请注意,我没有编程背景.

这是我到目前为止:

class BaseObject(object):
    def __init__(self, name=''):
        self._name = name


    def __repr__(self):
        return '<{0}: \'{1}\'>'.format(self.__class__.__name__, self.name)

    def _compare(self, other, *attributes):
        count = 0
        if isinstance(other, self.__class__):
            if len(attributes):
                for attrib in attributes:
                    if (attrib in self.__dict__.keys()) and (attrib in other.__dict__.keys()):
                        if self.__dict__[attrib] == other.__dict__[attrib]:
                            count += 1
                return (count == len(attributes))
            else:
                for attrib in self.__dict__.keys():
                    if (attrib in self.__dict__.keys()) and (attrib in other.__dict__.keys()):
                        if self.__dict__[attrib] == other.__dict__[attrib]:
                            count += 1
                return (count == len(self.__dict__.keys()))
    def _copy(self):
        return (copy.deepcopy(self))
Run Code Online (Sandbox Code Playgroud)

在添加到我的列表之前,我会执行以下操作:

found = False
for instance in myList:
    if instance._compare(newInstance): 
        found = True
        Break

if not found: myList.append(newInstance)
Run Code Online (Sandbox Code Playgroud)

但是我不清楚这是否是比较同一类实例内容的最有效或python-ic方式.

Mar*_*ers 6

改为使用__eq__特殊方法:

def __eq__(self, other, *attributes):
    if not isinstance(other, type(self)):
        return NotImplemented

    if attributes:
        d = float('NaN')  # default that won't compare equal, even with itself
        return all(self.__dict__.get(a, d) == other.__dict__.get(a, d) for a in attributes)

    return self.__dict__ == other.__dict__
Run Code Online (Sandbox Code Playgroud)

现在你可以使用:

if newInstance in myList:
Run Code Online (Sandbox Code Playgroud)

并且Python将自动使用__eq__特殊方法来测试相等性.

在我的版本中,我保留了传递一组有限属性的能力:

instance1.__eq__(instance2, 'attribute1', 'attribute2')
Run Code Online (Sandbox Code Playgroud)

all()用来确保我们只测试所需的数量.

请注意,我们返回NotImplemented一个特殊的单例对象,表示不支持比较; Python将询问另一个对象是否支持相等测试,而不是那种情况.


jon*_*rpe 5

您可以为您的类实现比较魔术方法 __eq__(self, other),然后就可以了

if instance == newInstance:
Run Code Online (Sandbox Code Playgroud)

由于您显然不知道您的实例将具有哪些属性,您可以执行以下操作:

def __eq__(self, other):
    return isinstance(other, type(self)) and self.__dict__ == other.__dict__
Run Code Online (Sandbox Code Playgroud)