擅长:python、mysql、java
<p>你所描述的就是我一直以来所做的。因为它是完全通用的,所以您总是可以将该功能分解为一个mixin类,并在需要该功能的类中继承它。</p>
<pre><code>class CommonEqualityMixin(object):
def __eq__(self, other):
return (isinstance(other, self.__class__)
and self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
class Foo(CommonEqualityMixin):
def __init__(self, item):
self.item = item
</code></pre>