擅长:python、mysql、java
<p>您应该重写<a href="https://docs.python.org/3/reference/datamodel.html#object.__eq__" rel="nofollow noreferrer">^{<cd1>}</a>方法,使其依赖于对象<code>id</code>,而不是其值。但是请注意,您的对象也必须是散列的,所以您也应该定义一个适当的<a href="https://docs.python.org/3/reference/datamodel.html#object.__hash__" rel="nofollow noreferrer">^{<cd3>}</a>方法。在</p>
<pre><code>class My_obj:
def __init__(self, val):
self.val = val
def __hash__(self):
return hash(self.val)
def __eq__(self, arg):
return id(self) == id(arg)
def __repr__(self):
return str(self.val)
</code></pre>
<p>演示:</p>
^{pr2}$