8.2.1 “Dangling else” Avoidance
避免“悬挂的else”
Python’s design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in code that is syntactically correct. One of those such problems is the (in)famous “dangling else” problem, a semantic optical illusion.
Python使用缩进而不是用大括号来为代码段设定边界的设计,不仅帮助强化了代码的正确性,而且暗中帮助避免了句法上正确的代码潜在的问题。其中一个问题就是(不)知名的“悬挂的else”问题,叫做语义视错觉。
We present some C code here to illustrate our example (which is also illu- minated by K&R and other programming texts):
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
我们在这里给出一段C代码作为我们的例子(K&R与其他的编程教材也给出过)
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
The question is, which if does the else belong to? In the C language, the rule is that the else stays with the closest if. In our example above, although indented for the outer if statement, the else statement really belongs to the inner if statement because the C compiler ignores superfluous whitespace. As a result, if you have a positive balance but it is below the minimum, you will get the horrid (and erroneous) message that your balance
is either zero or negative.
问题是:else 属于哪个if ?在C语言中,规则是else与最近的if搭配。在我们上面的例子中,else虽然是想和外层的if搭配,但是事实上else属于内部的if,因为C编译器会忽视掉多余的空白。结果,
Although solving this problem may be easy due to the simplistic nature of the example, any larger sections of code embedded within this framework may be a hair-pulling experience to root out. Python puts up guardrails not necessarily to prevent you from driving off the cliff, but to steer you away from danger. The same example in Python will result in one of the following choices (one of which is correct):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
虽然解决这个问题可能比较容易,但是是由于这个例子本事比较简单,任何庞大的代码中嵌有这样的代码可能得花费巨大大的精力才能找到。Python提供了工具不是一定要阻止您从悬崖上掉下去,而是带您离开危险的境地。在Python中相同的例子会成为下面选择中的一个(其中一个是正确的):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
Python’s use of indentation forces the proper alignment of code, giving the programmer the ability to make a conscious decision as to which if an else statement belongs to. By limiting your choices and thus reducing ambiguities, Python encourages you to develop correct code the first time. It is impossible to create a dangling else problem in Python. Also, since parentheses are not required, Python c8.2.1 “Dangling else” Avoidance
避免“悬挂的else”
Python’s design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in code that is syntactically correct. One of those such problems is the (in)famous “dangling else” problem, a semantic optical illusion.
Python使用缩进而不是用大括号来为代码段设定边界的设计,不仅帮助强化了代码的正确性,而且暗中帮助避免了句法上正确的代码潜在的问题。其中一个问题就是(不)知名的“悬挂的else”问题,叫做语义视错觉。
We present some C code here to illustrate our example (which is also illu- minated by K&R and other programming texts):
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
我们在这里给出一段C代码作为我们的例子(K&R与其他的编程教材也给出过)
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
The question is, which if does the else belong to? In the C language, the rule is that the else stays with the closest if. In our example above, although indented for the outer if statement, the else statement really belongs to the inner if statement because the C compiler ignores superfluous whitespace. As a result, if you have a positive balance but it is below the minimum, you will get the horrid (and erroneous) message that your balance
is either zero or negative.
问题是:else 属于哪个if ?在C语言中,规则是else与最近的if搭配。在我们上面的例子中,else虽然是想和外层的if搭配,但是事实上else属于内部的if,因为C编译器会忽视掉多余的空白。结果,
Although solving this problem may be easy due to the simplistic nature of the example, any larger sections of code embedded within this framework may be a hair-pulling experience to root out. Python puts up guardrails not necessarily to prevent you from driving off the cliff, but to steer you away from danger. The same example in Python will result in one of the following choices (one of which is correct):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
虽然解决这个问题可能比较容易,但是是由于这个例子本事比较简单,任何庞大的代码中嵌有这样的代码可能得花费巨大大的精力才能找到。Python提供了工具不是一定要阻止您从悬崖上掉下去,而是带您离开危险的境地。在Python中相同的例子会成为下面选择中的一个(其中一个是正确的):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
Python’s use of indentation forces the proper alignment of code, giving the programmer the ability to make a conscious decision as to which if an else statement belongs to. By limiting your choices and thus reducing ambiguities, Python encourages you to develop correct code the first time. It is impossible to create a dangling else problem in Python. Also, since parentheses are not required, Python code is easier to read.
Python缩进的使用强制代码的正确对齐,给与了程序员清醒决定else属于哪一个if的能力。通过限制您的选择从而减少不确定性,Python鼓励您第一次就写出正确的代码。在Python中制造出悬挂的else问题是不可能的,而且由于括号不再被使用,Python代码更加易读。
8.2.1 “Dangling else” Avoidance
避免“悬挂的else”
Python’s design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in code that is syntactically correct. One of those such problems is the (in)famous “dangling else” problem, a semantic optical illusion.
Python使用缩进而不是用大括号来为代码段设定边界的设计,不仅帮助强化了代码的正确性,而且暗中帮助避免了句法上正确的代码潜在的问题。其中一个问题就是(不)知名的“悬挂的else”问题,叫做语义视错觉。
We present some C code here to illustrate our example (which is also illu- minated by K&R and other programming texts):
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
我们在这里给出一段C代码作为我们的例子(K&R与其他的编程教材也给出过)
/* dangling-else in C */
if (balance > 0.00)
if (((balance - amt) > min_bal) && (atm_cashout() == 1))
printf("Here's your cash; please take all bills.\n");
else
printf("Your balance is zero or negative.\n");
The question is, which if does the else belong to? In the C language, the rule is that the else stays with the closest if. In our example above, although indented for the outer if statement, the else statement really belongs to the inner if statement because the C compiler ignores superfluous whitespace. As a result, if you have a positive balance but it is below the minimum, you will get the horrid (and erroneous) message that your balance
is either zero or negative.
问题是:else 属于哪个if ?在C语言中,规则是else与最近的if搭配。在我们上面的例子中,else虽然是想和外层的if搭配,但是事实上else属于内部的if,因为C编译器会忽视掉多余的空白。结果,
Although solving this problem may be easy due to the simplistic nature of the example, any larger sections of code embedded within this framework may be a hair-pulling experience to root out. Python puts up guardrails not necessarily to prevent you from driving off the cliff, but to steer you away from danger. The same example in Python will result in one of the following choices (one of which is correct):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
虽然解决这个问题可能比较容易,但是是由于这个例子本事比较简单,任何庞大的代码中嵌有这样的代码可能得花费巨大大的精力才能找到。Python提供了工具不是一定要阻止您从悬崖上掉下去,而是带您离开危险的境地。在Python中相同的例子会成为下面选择中的一个(其中一个是正确的):
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
or
if balance > 0.00:
if balance - amt > min_bal and atm_cashout():
print "Here's your cash; please take all bills."
else:
print 'Your balance is zero or negative.'
Python’s use of indentation forces the proper alignment of code, giving the programmer the ability to make a conscious decision as to which if an else statement belongs to. By limiting your choices and thus reducing ambiguities, Python encourages you to develop correct code the first time. It is impossible to create a dangling else problem in Python. Also, since parentheses are not required, Python code is easier to read.
Python缩进的使用强制代码的正确对齐,给与了程序员清醒决定else属于哪一个if的能力。通过限制您的选择从而减少不确定性,Python鼓励您第一次就写出正确的代码。在Python中制造出悬挂的else问题是不可能的,而且由于括号不再被使用,Python代码更加易读。
转载于:https://blog.51cto.com/lyncmaster/472331