有时我会把if中的长条件分解成几行。最明显的方法是:
if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
视觉上不是很吸引人,因为动作与环境融为一体。然而,这是使用4个空格的正确Python缩进的自然方式。
目前我正在使用:
if ( cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
但这不是很漂亮
你能推荐另一种方法吗?
我认为@zkanda的解决方案稍加改动会很好。如果您在各自的列表中列出了条件和值,则可以使用列表理解来进行比较,这将使添加条件/值对的情况更加普遍。
conditions = [1, 2, 3, 4]
values = [1, 2, 3, 4]
if all([c==v for c, v in zip(conditions, values)]):
# do something
如果我真的想对这样的语句进行硬编码,为了易读,我会这样写:
if (condition1==value1) and (condition2==value2) and \
(condition3==value3) and (condition4==value4):
只需要用iand运算符抛出另一个解决方案:
proceed = True
for c, v in zip(conditions, values):
proceed &= c==v
if proceed:
# do something
为了完整起见,只需要一些其他随意的想法。如果它们对你有用,就使用它们。否则,你最好尝试其他方法。
你也可以用字典这样做:
>>> 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'
最后两个我还没有测试过,但如果你想使用这些概念,那么这些概念应该足以让你继续下去。
(顺便说一句,如果这只是一次性的事情,你可能会更好地使用你最初提出的方法。如果你在很多地方进行比较,这些方法可能会增强可读性,让你不会因为它们有点粗糙而感到很糟糕。)
这是我个人的看法:长条件(在我看来)是一种代码气味,建议重构为布尔返回函数/方法。例如:
def is_action__required(...):
return (cond1 == 'val1' and cond2 == 'val2'
and cond3 == 'val3' and cond4 == 'val4')
现在,如果我找到了一种让多行条件看起来很好的方法,我可能会发现自己满足于拥有它们,并跳过重构。
另一方面,让它们扰乱了我的审美意识,这是一种重构的激励。
因此,我的结论是,多行条件应该看起来很难看,这是避免它们的一种激励。
(我对标识符进行了轻微修改,因为固定宽度的名称不能代表真实代码——至少不能代表我遇到的真实代码——并且会掩盖示例的可读性。)
if (cond1 == "val1" and cond22 == "val2"
and cond333 == "val3" and cond4444 == "val4"):
do_something
这适用于“和”和“或”(重要的是它们位于第二行的第一位),但对于其他较长的条件就更不适用了。幸运的是,前者似乎是更常见的情况,而后者往往很容易用临时变量重写。(这通常不难,但在重写时保持“和”/“或”的短路可能很难或不那么明显/可读。)
由于我在你的关于C++的博客文章中发现了这个问题,我将补充我的C++风格是相同的:
if (cond1 == "val1" and cond22 == "val2"
and cond333 == "val3" and cond4444 == "val4") {
do_something
}
请原谅我的无知,但我对#Python的了解不如在座的任何人,但我碰巧在3D BIM建模中编写自己的对象脚本时发现了类似的东西,因此我将调整我的算法以适应Python。
我在这里发现的问题是双面的:
我的价值观对于试图破译剧本的人来说似乎很陌生。如果这些值被更改(最有可能),或者如果必须添加新条件(破坏模式),代码维护将付出高昂的代价
要绕过所有这些问题,脚本必须这样运行
param_Val01 = Value 01 #give a meaningful name for param_Val(i) preferable an integer
param_Val02 = Value 02
param_Val03 = Value 03
param_Val04 = Value 04 # and ... etc
conditions = 0 # this is a value placeholder
########
Add script that if true will make:
conditions = conditions + param_Val01 #value of placeholder is updated
########
### repeat as needed
if conditions = param_Val01 + param_Val02 + param_Val03 + param_Val04:
do something
这种方法的优点:
脚本可读。脚本很容易维护。条件是对表示期望条件的值的和的1比较操作。无需多级条件
希望它能帮助你们