完全を期すために、他のいくつかのランダムなアイデアを示します。それらがあなたのために働くなら、それらを使用してください。それ以外の場合は、おそらく別のことを試したほうがよいでしょう。
辞書を使用してこれを行うこともできます。
>>> x = {'cond1' : 'val1', 'cond2' : 'val2'}
>>> y = {'cond1' : 'val1', 'cond2' : 'val2'}
>>> x == y
True
このオプションはより複雑ですが、役に立つ場合もあります。
class Klass(object):
def __init__(self, some_vars):
#initialize conditions here
def __nonzero__(self):
return (self.cond1 == 'val1' and self.cond2 == 'val2' and
self.cond3 == 'val3' and self.cond4 == 'val4')
foo = Klass()
if foo:
print "foo is true!"
else:
print "foo is false!"
それがうまくいくかどうかはわかりませんが、考慮すべき別のオプションです。もう1つの方法は次のとおりです。
class Klass(object):
def __init__(self):
#initialize conditions here
def __eq__(self):
return (self.cond1 == 'val1' and self.cond2 == 'val2' and
self.cond3 == 'val3' and self.cond4 == 'val4')
x = Klass(some_values)
y = Klass(some_other_values)
if x == y:
print 'x == y'
else:
print 'x!=y'
最後の 2 つはまだテストしていませんが、目的があれば十分に理解できるはずです。
(記録として、これが 1 回限りのことである場合は、最初に提示した方法を使用した方がよいでしょう。多くの場所で比較を行っている場合、これらの方法により読みやすさが向上し、彼らが一種のハッキーであるという事実についてそれほど気分が悪いわけではありません。)