为了完整起见,只是其他一些随机的想法。如果它们对您有用,请使用它们。否则,您最好尝试其他方法。
你也可以用字典来做到这一点:
>>> 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!"
不知道这是否适合您,但这是另一个需要考虑的选择。这是另一种方法:
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'
最后两个我没有测试过,但如果你想要这样做,这些概念应该足以让你继续前进。
(为了记录,如果这只是一次性的事情,你可能最好使用你一开始提出的方法。如果你在很多地方进行比较,这些方法可能会增强可读性,足以让你对他们有点老套这一事实并不感到难过。)