for:else: - else子句的任何实际用途? [英] for: else: - any practical uses for the else clause?

查看:72
本文介绍了for:else: - else子句的任何实际用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常古老的主题:
http://groups.google.com/group/comp=\"42d2041257c47e


讨论了可选的else: for语句的条款。


我想知道是否有人曾经为其他的

分支找到实际用途?

解决方案



metaperl我想知道是否有人找到了实际用途

metaperlelse分店?


是的,我经常使用它:


for foo in bar:

如果foo符合某些条件:

打印航行到大溪地!

休息

否则:

打印弃船!


Skip


我********** @ gmail.com écrit:


A very旧帖子:
http://groups.google.com/ group / comp .... 42d2041257c47e


讨论了可选的else: for语句的条款。


我想知道是否有人找到了其他的实际用途

分支?



我经常在以下结构中使用它:


表示someList中的值:

if someCondition(value):

打印找到匹配项目

休息

否则:

打印无匹配项目

返回False


#...继续价值


-

Amaury


我******** **@gmail.com écrit:


一个非常古老的主题:
http:// groups.google.com/group/comp....42d2041257c47e


讨论了该选项alelse: for语句的条款。


我想知道是否有人找到了其他的实际用途

分支?



< aol>

就像Skip和Amaury ...

< / aol>


A very old thread:
http://groups.google.com/group/comp....42d2041257c47e

discusses the optional "else:" clause of the for statement.

I''m wondering if anyone has ever found a practical use for the else
branch?

解决方案


metaperlI''m wondering if anyone has ever found a practical use for the
metaperlelse branch?

Yeah, I use it from time to time:

for foo in bar:
if foo matches some condition:
print "sail to tahiti!"
break
else:
print "abandon ship!"

Skip


me**********@gmail.com a écrit :

A very old thread:
http://groups.google.com/group/comp....42d2041257c47e

discusses the optional "else:" clause of the for statement.

I''m wondering if anyone has ever found a practical use for the else
branch?

I use it regularly in contructs like:

for value in someList:
if someCondition(value):
print "a matching item was found"
break
else:
print "no matching item"
return False

# ... continue with value

--
Amaury


me**********@gmail.com a écrit :

A very old thread:
http://groups.google.com/group/comp....42d2041257c47e

discusses the optional "else:" clause of the for statement.

I''m wondering if anyone has ever found a practical use for the else
branch?

<aol>
Just like Skip and Amaury...
</aol>


这篇关于for:else: - else子句的任何实际用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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