为什么python在for和while循环之后使用“else”? [英] Why does python use 'else' after for and while loops?

查看:161
本文介绍了为什么python在for和while循环之后使用“else”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个构造是如何工作的:

b $ b print(i)

if i == 9:
print(太大了 - 我放弃了!)
break;
其他:
print(Completed successfully)

不用理解为什么 else 在这里被用作关键字,因为它表明只有块中的块没有完成,这与它所做的相反!不管我怎么想,我的大脑都无法从 for 语句顺畅地进行到 else 块。对我来说,继续或者 continuewith 会更有意义(我正在努力训练自己读取它, )。

我想知道Python编程人员如何在头脑中读取这个构造(或者大声朗读,如果你喜欢的话)。也许我错过了一些可以使这样的代码块更容易辨认的东西?解析方案

这是一个奇怪的结构,即使是经验丰富的Python编码人员。当与for循环一起使用时,它基本上意味着在迭代中找到某个项目,否则如果没有找到......。如:

  found_obj = None 
对象中的obj:
if obj.key == search_key :
found_obj = obj
break
else:
print'找不到对象'

但是,无论何时你看到这个构造,更好的选择是将搜索封装在一个函数中:

  def find_obj(search_key):
对象中的obj:
if obj.key == search_key:
return obj


$ b

或者使用列表理解:

pre $匹配_objs = o在对象中,如果o.key == search_key]
如果matching_objs:
print'找到',matching_objs [0]
else:
print'找不到对象'

与其他两个版本在语义上并不相同,但对于非性能严重的代码无论你是否迭代整个列表都没有关系。其他人可能不同意,但我个人会避免在生产代码中使用for-else或while-else块。

另见 [Python-ideas] for ... else threads的摘要


I understand how this construct works:

for i in range(10):
    print(i)

    if i == 9:
        print("Too big - I'm giving up!")
        break;
else:
    print("Completed successfully")

But I don't understand why else is used as the keyword here, since it suggests the code in question only runs if the for block does not complete, which is the opposite of what it does! No matter how I think about it, my brain can't progress seamlessly from the for statement to the else block. To me, continue or continuewith would make more sense (and I'm trying to train myself to read it as such).

I'm wondering how Python coders read this construct in their head (or aloud, if you like). Perhaps I'm missing something that would make such code blocks more easily decipherable?

解决方案

It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:

found_obj = None
for obj in objects:
    if obj.key == search_key:
        found_obj = obj
        break
else:
    print 'No object found.'

But anytime you see this construct, a better alternative is to either encapsulate the search in a function:

def find_obj(search_key):
    for obj in objects:
        if obj.key == search_key:
            return obj

Or use a list comprehension:

matching_objs = [o for o in objects if o.key == search_key]
if matching_objs:
    print 'Found', matching_objs[0]
else:
    print 'No object found.'

It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.

See also [Python-ideas] Summary of for...else threads

这篇关于为什么python在for和while循环之后使用“else”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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