擅长:python、mysql、java
<p><code>zip</code>只能到达最短的列表。你想要<code>itertools.zip_longest</code>,你不想要<code>.val</code>(你的迭代器已经返回了实际值)。试试这个:</p>
<pre><code>def __eq__(self, other):
for val1, val2 in zip_longest(self, other):
if val1 != val2:
return False
return True
</code></pre>
<p>或者更好?你知道吗</p>
<pre><code>def __eq__(self, other):
return all(val1 == val2 for val1, val2 in zip_longest(self, other))
</code></pre>
<p><strong>编辑</p>
<p>我喜欢@BrenBarn建议先检查长度。这里有一个更有效的答案:</p>
<pre><code>def __eq__(self, other):
return len(self) == len(other) and all(
val1 == val2 for val1, val2 in zip(self, other))
</code></pre>