换别的 [英] for-else

查看:64
本文介绍了换别的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,在Python中,我几乎讨厌for循环的其他:

- 我有问题要记住它的含义;

- 当我后来想将Python

代码翻译成其他语言时,它给我带来了一些小问题(你总是需要翻译长寿命的
代码)。 br />
- 到目前为止我只使用过一次。


所以到目前为止我还是希望从Python 3.0中删除它。


然后这篇文章:
http ://tratt.net/laurie/tech_article...guage_features

告诉我,我对''for'的'else'的问题主要来自于/>
来自其错误的命名。融合语言是另一种非常类似于Python的类似语言,并且它使用了更好的命名(单词

耗尽这个词很长并且非拼写错误-English

发言者,所以它并不完美):


for ...:

... < br $> b $ b筋疲力尽:

......

坏了:

...


含义明确。而其他则是似乎在那里意义不大。

所以我可能喜欢类似于Python 3.x(或删除

else)。


再见,

bearophile

解决方案

2008年3月4日星期二4:17 PM,Carl Banks< pa ************ @ gmail.comwrote:


for。 ..:

...

筋疲力尽:

...

破损:

...

>

含义明确。而其他则是似乎在那里意义不大。

所以我可能喜欢Python 3.x的类似内容(或删除

else)。




我不会因为它自身的优点而反对这一点,但是名称背后有一个

的理由。否则" ;.如果你认为for循环是一个

汇总如果... elif ... else语句(这种情况这是

合理的往往是相同的其他人会有用),然后

else在for循环中,子句将保持不变。


例如,如果你有一个(琐碎的)if ... elif ...其他像这样:

如果a == 0:

do_task_0()

elif a == 1:

do_task_1()

elif a == 2:

do_task_2()

else:

do_default_task()

您可以将其汇总成一个for ... else语句,如下所示:


for i in range(3):

if a == i:

do_task [a]()

else:

do_default_task()



你忘记了休息声明。在此循环中,else套件将始终执行

。有点证明了熊人的意思,因为 - 其他的真的是

棘手。

-

mvh Bj?rn


例如,如果你有一个(琐碎的)if ... elif ...其他像这样:


>

如果a == 0:

do_task_0()

elif a == 1:

do_task_1()

elif a == 2:

do_task_2()

else:

do_default_task()


您可以将它汇总成一个for ... else语句,如下所示:


for i in range(3):

如果a == i:

do_task [a]()



重要的破解在这里失踪...


else:

do_default_task()



或者这个代码将do_task_i *和* do_default_task()......


-tkc


3月4日,10日:上午55点,BJ?林德奎斯特 < bjou ... @ gmail.comwrote:


2008年3月4日星期二下午4:17,Carl Banks< pavlovevide ... @ gmail .comwrote:


for ...:

...

筋疲力尽:

...

坏了:

...


含义是明确的。而其他则是似乎在那里意义不大。

所以我可能喜欢Python 3.x的类似内容(或删除

else)。


我不会因为它自身的优点而反对这个,但是有一个

背后的理由名称else。如果你认为for循环是一个

汇总如果... elif ... else语句(这种情况这是

合理的往往是相同的其他人会有用),然后

else子句在for循环中保持不变。


例如,如果你有一个(琐碎的)if ... elif ...其他像这样:


如果a == 0:

do_task_0()

elif a == 1:

do_task_1()

elif a == 2:

do_task_2()

else:

do_default_task()


您可以将其汇总成for ... else语句,如下所示:


for i in range(3):

if a == i:

do_task [a]()

否则:

do_default_task()



你忘记了break语句。在此循环中,else套件将始终执行

。有点证明了熊人的观点,因为 - 其他的确非常尴尬。



啊哈,但无论有没有其他

条款都会出错......

Carl Banks


So far in Python I''ve almost hated the ''else'' of the ''for'' loops:
- I have problems to remember its meaning;
- It gives me little problems when I later want to translate Python
code to other languages (and you always have to translate long-lived
code).
- I have used it only once, so far.

So so far I''d liked to see it removed from Python 3.0.

But then this article:
http://tratt.net/laurie/tech_article...guage_features
has shown me that my problems with the ''else'' of the ''for'' mostly come
from just its bad naming. The converge language is yet another very
Python-like language, and it uses a better naming (the word
"exhausted" is long and has a complex spelling for non-English
speakers, so it''s not perfect):

for ...:
...
exhausted:
...
broken:
...

The meaning is explicit. While "else" seems to mean little there.
So I may like something similar for Python 3.x (or the removal of the
"else").

Bye,
bearophile

解决方案

On Tue, Mar 4, 2008 at 4:17 PM, Carl Banks <pa************@gmail.comwrote:

for ...:
...
exhausted:
...
broken:
...
>
The meaning is explicit. While "else" seems to mean little there.
So I may like something similar for Python 3.x (or the removal of the
"else").



I would not be opposed to this on its own merits, but there is a
rationale behind the name "else". If you consider a for loop to be a
rolled-up if...elif...else statement (situations where this is
reasonable tend to be the same ones were else would be useful), then
the "else" clause would remain unchanged on the for loop.

For instance, if you have a (trivial) if...elif...else like this:

if a == 0:
do_task_0()
elif a == 1:
do_task_1()
elif a == 2:
do_task_2()
else:
do_default_task()

You could roll it up into a for...else statement like this:

for i in range(3):
if a == i:
do_task[a]()
else:
do_default_task()

You forgot the break statement. The else suite will always be executed
in this loop. Kind of proves bearophiles point, for-else is really
tricky.
--
mvh Bj?rn


For instance, if you have a (trivial) if...elif...else like this:

>
if a == 0:
do_task_0()
elif a == 1:
do_task_1()
elif a == 2:
do_task_2()
else:
do_default_task()

You could roll it up into a for...else statement like this:

for i in range(3):
if a == i:
do_task[a]()

important "break" missing here...

else:
do_default_task()


or otherwise this code will do_task_i *and* do_default_task()...

-tkc


On Mar 4, 10:55 am, "BJ?rn Lindqvist" <bjou...@gmail.comwrote:

On Tue, Mar 4, 2008 at 4:17 PM, Carl Banks <pavlovevide...@gmail.comwrote:

for ...:
...
exhausted:
...
broken:
...

The meaning is explicit. While "else" seems to mean little there.
So I may like something similar for Python 3.x (or the removal of the
"else").

I would not be opposed to this on its own merits, but there is a
rationale behind the name "else". If you consider a for loop to be a
rolled-up if...elif...else statement (situations where this is
reasonable tend to be the same ones were else would be useful), then
the "else" clause would remain unchanged on the for loop.

For instance, if you have a (trivial) if...elif...else like this:

if a == 0:
do_task_0()
elif a == 1:
do_task_1()
elif a == 2:
do_task_2()
else:
do_default_task()

You could roll it up into a for...else statement like this:

for i in range(3):
if a == i:
do_task[a]()
else:
do_default_task()


You forgot the break statement. The else suite will always be executed
in this loop. Kind of proves bearophiles point, for-else is really
tricky.

Ah ha, but that would have been a mistake with or without the else
clause....
Carl Banks


这篇关于换别的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆