名、ミドルネーム、姓の属性を持つ Person クラスがあるとします。Person オブジェクトに対して 2 つの異なるタイプの等価性チェックを実行できるようにしたいと考えています。
- すべての属性の文字列比較に基づいて正確に等しい
- 矛盾していない、つまり "G. Bluth" == "George Oscar Bluth"
__eq__
私はこれのためにand を__ne__
別々に使用するというアイデアをいじっています:
Person('g', '', 'bluth') == Person('george', 'oscar', 'bluth') # False
Person('g', '', 'bluth') != Person('george', 'oscar', 'bluth') # False
それはきちんとした解決策のように思えますが、!=
常に逆を返すとは限らないので、==
私は緊張します. それは悪い習慣と見なされますか?演算子の使用を避けて、次のようなメソッドを使用する必要がありconsistent(self, other)
ますか?
実装例:
class Person(object):
def __init__(self, first, middle, last):
self.first = first
self.middle = middle
self.last = last
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return NotImplemented
def __ne__(self, other):
if type(other) is type(self):
return not (self._compatible(self.first, other.first) and
self._compatible(self.middle, other.middle) and
self._compatible(self.last, other.last))
return NotImplemented
def _compatible(self, s, o):
if s and o:
if s == o or (len(s) == 1 and s == o[0]) or (len(o) == 1 and o == s[0]):
return True
return False
return True