如何退出 if 子句

过早退出 if子句有哪些方法?

有时候我在编写代码时,希望将 break语句放在 if子句中,只是为了记住这些语句只能用于循环。

让我们以下面的代码为例:

if some_condition:
...
if condition_a:
# do something
# and then exit the outer if block
...
if condition_b:
# do something
# and then exit the outer if block
# more code here

我可以想到一种方法: 假设退出情况发生在嵌套的 if 语句中,将剩余的代码封装在 big else 块中。例如:

if some_condition:
...
if condition_a:
# do something
# and then exit the outer if block
else:
...
if condition_b:
# do something
# and then exit the outer if block
else:
# more code here

这样做的问题是,更多的出口位置意味着更多的嵌套/缩进代码。

或者,我可以编写代码,使 if子句尽可能小,而且不需要任何退出。

有人知道一个更好的方法来退出 if条款吗?

如果有任何关联的 else-If 和 else 子句,我认为退出会跳过它们。

449070 次浏览

(这种方法适用于 if、多个嵌套循环和其他 break无法轻易实现的构造。)

用自己的函数包装代码。使用 return代替 break

例如:

def some_function():
if condition_a:
# do something and return early
...
return
...
if condition_b:
# do something else and return early
...
return
...
return


if outer_condition:
...
some_function()
...

可能是这个?

if some_condition and condition_a:
# do something
elif some_condition and condition_b:
# do something
# and then exit the outer if block
elif some_condition and not condition_b:
# more code here
else:
#blah
if

实际上,你所描述的是 goto 语句,通常被严厉批评。第二个例子更容易理解。

不过,更为清洁的还是:

if some_condition:
...
if condition_a:
your_function1()
else:
your_function2()


...


def your_function2():
if condition_b:
# do something
# and then exit the outer if block
else:
# more code here
from goto import goto, label


if some_condition:
...
if condition_a:
# do something
# and then exit the outer if block
goto .end
...
if condition_b:
# do something
# and then exit the outer if block
goto .end
# more code here


label .end

(请不要用这个)

你可以模拟 goto 的功能,但有一些例外:

try:
# blah, blah ...
# raise MyFunkyException as soon as you want out
except MyFunkyException:
pass

免责声明: 我只是想提请你们注意这样做事情的 可能性,而在正常情况下,我绝不认可它是合理的。正如我在关于这个问题的评论中所提到的,到目前为止,最好首先对代码进行结构化,以避免使用拜占庭式条件句。:-)

一般来说,不要这样做。如果你正在嵌套“如果”和打破他们,你正在做错误的。

但是,如果你必须这样做:

if condition_a:
def condition_a_fun():
do_stuff()
if we_wanna_escape:
return
condition_a_fun()
if condition_b:
def condition_b_fun():
do_more_stuff()
if we_wanna_get_out_again:
return
condition_b_fun()

注意,函数不必在 if 语句中声明,它们可以提前声明;)这将是一个更好的选择,因为它将避免重构出一个丑陋的 if/then。

while some_condition:
...
if condition_a:
# do something
break
...
if condition_b:
# do something
break
# more code here
break

对于实际要求的内容,我的方法是将这些 if放入一个单循环中

while (True):
if (some_condition):
...
if (condition_a):
# do something
# and then exit the outer if block
break
...
if (condition_b):
# do something
# and then exit the outer if block
break
# more code here
# make sure it is looped once
break

测试一下:

conditions = [True,False]
some_condition = True


for condition_a in conditions:
for condition_b in conditions:
print("\n")
print("with condition_a", condition_a)
print("with condition_b", condition_b)
while (True):
if (some_condition):
print("checkpoint 1")
if (condition_a):
# do something
# and then exit the outer if block
print("checkpoint 2")
break
print ("checkpoint 3")
if (condition_b):
# do something
# and then exit the outer if block
print("checkpoint 4")
break
print ("checkpoint 5")
# more code here
# make sure it is looped once
break

所以我知道你想冲出 如果代码块的外围

if some_condition:
...
if condition_a:
# do something
# and then exit the outer if block
...
if condition_b:
# do something
# and then exit the outer if block
# more code here

解决这个问题的一种方法是,您可以在外部 if 块中测试一个 false 条件,然后该条件将隐式退出代码块,然后使用 else 块将其他 if 嵌套到 做点什么

if test_for_false:
# Exit the code(which is the outer if code)


else:
if condition_a:
# Do something


if condition_b:
# Do something

在 if 条件中使用 return将从函数返回, 这样您就可以使用 return 来中断 if 条件。

还有一种方法不依赖于定义函数(因为有时候对于小的代码片段来说,函数的可读性较低) ,不使用额外的外层 while 循环(这可能需要在注释中特别注意,以便第一眼就能理解) ,不使用 goto (...) ,最重要的是让你保持外层的缩进级别,如果这样的话,你就不必开始嵌套东西了。

if some_condition:
...
if condition_a:
# do something
exit_if=True # and then exit the outer if block
if some condition and not exit_if: # if and only if exit_if wasn't set we want to execute the following code
# keep doing something
if condition_b:
# do something
exit_if=True # and then exit the outer if block
if some condition and not exit_if:
# keep doing something

是的,这也需要重新审视可读性,但是,如果代码片段很小,就不需要跟踪任何永远不会重复的 while 循环,在理解了中间 if 的用途之后,就很容易阅读了,所有代码都在一个地方,并且具有相同的缩进。

而且应该很有效率。

还有一个办法。它使用一个单一的项 for 循环,使您可以只使用继续。它可以防止无缘无故地需要额外的函数。另外还消除了势无穷的 while 循环。

if something:
for _ in [0]:
# Get x
if not x:
continue


# Get y
if not y:
continue


# Get z
if not z:
continue


# Stuff that depends on x, y, and z

在不使用其他方法的情况下,应用这种方法的唯一方法是 elif,如下面的示例所示

a = ['yearly', 'monthly', 'quartly', 'semiannual', 'monthly', 'quartly', 'semiannual', 'yearly']
# start the condition
if 'monthly' in b:
print('monthly')
elif 'quartly' in b:
print('quartly')
elif 'semiannual' in b:
print('semiannual')
elif 'yearly' in b:
print('yearly')
else:
print('final')

我浏览了一遍,没有人提到这个技巧。与其他解决方案相比,它非常简单,如果您不想使用 try-ex 语句,那么这可能是您的最佳选择。

#!/usr/bin/python3
import sys


foo = 56


if (foo != 67):
print("ERROR: Invalid value for foo")
sys.exit(1)