Python:自定义类上__eq__和__contains__的奇怪行为

时间:2016-03-26 18:39:40

标签: python contains equals-operator

我有以下自定义类(剥离了一些),实现了一个表达式树:

from abc import ABC, abstractmethod


class Operator:
    def __init__(self, str_reps):
        self.str_reps = str_reps

    def __str__(self):
        return self.str_reps[0]

    def __eq__(self, other):
        return self is other

    def __ne__(self, other):
        return self is not other

    def __hash__(self):
        return hash(str(self))


NOT = Operator(["¬", "~", "not", "!"])
AND = Operator(["∧", "&", "and"])
OR = Operator(["∨", "|", "or"])


class Node(ABC):
    @abstractmethod
    def __eq__(self, other):
        pass

    @abstractmethod
    def __hash__(self):
        pass

    def __ne__(self, other):
        return not self == other

    @abstractmethod
    def __str__(self):
        pass

    @abstractmethod
    def __invert__(self):
        pass

    def bracket_if_necessary(self):
        return str(self)


class Leaf(Node):
    def __init__(self, v):
        self.val = v

    def __eq__(self, other):
        if not isinstance(other, Leaf):
            return False
        return self.val == other.val

    def __hash__(self):
        return hash(self.val)

    def __str__(self):
        return str(self.val)

    def __invert__(self):
        return UnaryNode(self)


class UnaryNode(Node):
    def __init__(self, child):
        self.child = child
        self.hash = hash(NOT) + hash(self.child)

    def __eq__(self, other):
        if not isinstance(other, UnaryNode):
            return False
        return self.child == other.child

    def __hash__(self):
        return self.hash

    def __str__(self):
        return str(NOT) + self.child.bracket_if_necessary()

    def __invert__(self):
        return self.child


class VariadicNode(Node):
    def __init__(self, op, children):
        self.op = op
        self.children = children
        self.hash = hash(self.op) + sum(hash(child) for child in self.children)

    def __eq__(self, other):
        if not isinstance(other, VariadicNode):
            return False
        return self.op is other.op and set(self.children) == set(other.children)

    def __hash__(self):
        return self.hash

    def __str__(self):
        return (" " + str(self.op) + " ").join(child.bracket_if_necessary() for child in self)

    def __invert__(self):
        return VariadicNode(AND if self.op is OR else OR, tuple(~c for c in self))

    def bracket_if_necessary(self):
        return "(" + str(self) + ")"

    def __iter__(self):
        return iter(self.children)

    def __contains__(self, item):
        return item in self.children

如果我运行此功能并尝试

Leaf("36") == Leaf("36)
~Leaf("36") == ~Leaf("36")
~~Leaf("36") == Leaf("36")

他们都按预期返回True。

但是,我在使用这些节点的代码中遇到了错误:

# Simplify procedure in DPLL Algorithm
def _simplify(cnf, l):
    # print for debugging
    for c in cnf:
        print("b", l, ~l, type(l), "B", c, type(c), l in c)

    return VariadicNode(AND, tuple(_filter(c, ~l) for c in cnf if l not in c))


# Removes the chosen unit literal (negated above) from clause c
def _filter(c, l):
    # print for debugging
    for x in c:
        print("a", l, type(l), "A", x, type(x), x==l)

    return VariadicNode(c.op, tuple(x for x in c if x != l))

此处cnfVariadicNode(AND)的形式提供,所有孩子都为VariadicNode(OR)VariadicNode的孩子总是作为元组给出。

这两个印刷品的结果如下:

a ¬25 <class 'operators.UnaryNode'> A ¬25 <class 'operators.UnaryNode'> False
b ¬25 25 <class 'operators.UnaryNode'> B ¬25 ∨ ¬36 <class 'operators.VariadicNode'> False

不应该发生(第一行¬25 == ¬25而第二行¬25 in (¬25 ∨ ¬36)都应该返回True。但是输出中还有一行:

b ¬25 25 <class 'operators.UnaryNode'> B ¬25 <class 'operators.VariadicNode'> True

所以检查¬25 in (¬25)实际上确实按原样返回True。

谁能告诉我发生了什么?

如果需要更多信息,其他代码可以在[已删除]中找到(希望它已公开发布,我对github很新,所以我不知道他们的政策) 。请注意,它仍然是WIP。

这些类位于operators.py,其余(相关)代码位于SAT_solver.py,而test.py允许轻松运行整个项目,前提是networkx库是安装。

修改

我现在已将示例dimacs .txt文件推送到github存储库,导致出现上述问题。只需将hamilton.txtSAT_solver.pyoperators.py从存储库下载到同一文件夹,运行SAT_solver.py(具有main()方法)并输入hamilton或当提示输入问题文件名时,hamilton.txt到命令行(当提示阻止程序写入任何文件时,只需将解决方案文件名留空)。这应该导致大量输出,包括如上所述的有问题的行。

1 个答案:

答案 0 :(得分:1)

您发布的代码不是您的github repo中的代码。您的代码的UnaryNode.__eq__

def __eq__(self, other):
    if not isinstance(other, UnaryNode):
        return False
    return self.child == other.child

回购代码

def __eq__(self, other):
    if not isinstance(other, UnaryNode):
        return False
    return self.op is other.op and self.child == other.child

这也要求运营商是相同的。检测代码并破坏故障表明您在某处生成了两个不同的NOT运算符:

>>> str(x)
'¬24'
>>> str(l)
'¬24'
>>> x == l
False
>>> x.child == l.child
True
>>> str(x.op)
'¬'
>>> str(l.op)
'¬'
>>> x.op == l.op
False
>>> id(x.op)
2975964300
>>> id(l.op)
2976527276

追踪正在发生的事情并按照您的喜好进行修复,无论是避免多于一个还是不关心是否有多个(我的偏好)。我知道你在删除的评论中写道“运算符是单个对象,我希望根据它们的引用来比较它们”,但是(1)它们不是单个对象,(2)如果你不想要这样一件不必要的事情,你不会有麻烦......

如果我不得不猜测,当你致电copy.deepcopy时,会引入其他运营商,但你绝对不会与单身人士合作。