何时在Python中使用if vs elif [英] when to use if vs elif in python

查看:129
本文介绍了何时在Python中使用if vs elif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有多个条件语句的函数,则每个分支都将从该函数返回。我应该使用多个if语句还是if / elif / else?例如,说我有一个函数:

  def example(x):
如果x> 0:如果x<
返回正
0:
返回负
返回零

是否更好写下:

  def示例(x):
如果x> 0:
返回正数
elif x< 0:
返回负
否则:
返回零

两者都具有相同的结果,但是效率更高或被认为比其他人更惯用?



编辑:



在第一个示例中,两个人都说总是对if语句进行评估,对我而言似乎并非如此



例如,如果我运行代码:

  l = [1,2,3] 

def测试(a):
,如果> 0:
如果a>返回
。 2:
l.append(4)

测试(5)

l仍将等于[1,2,3]

解决方案

我将在评论中扩展我的评论。



在所有情况都返回的情况下,这些确实是等效的。在它们之间进行选择时重要的是更具可读性。



您的后一个示例使用 elif 结构明确指出案件是相互排斥的,而不是依靠事实是从收益中隐含的。这样可以使该信息更明显,从而使代码更易于阅读,并且不易出错。



例如,有人认为还有另一种情况:



  def示例(x):
如果x> 0:如果x == -15,则
返回正值
:如果x <= $ -15,则
print(特例!)
0:
返回负
返回零

突然如果用户希望这种情况是互斥的,则可能是一个潜在的错误(显然,在示例中这没有多大意义,但在更现实的情况下可能会出现)。如果使用 elif 可以消除这种歧义,并且使行为对于添加代码的人员可见,且其添加级别可能很高。 p>

如果我遇到第一个代码示例,我可能会假设选择使用 if 而不是 elifs 表示案例不是互斥的,因此需要更改 x 可能会用来更改如果执行的操作(显然,在这种情况下,意图是显而易见的,并且相互排斥,但是,在此,我们要说的不是那么明显的情况-一致性是很好,所以即使在一个简单的例子中也很明显,最好还是坚持一种方法。


If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function:

def example(x):
    if x > 0:
        return 'positive'
    if x < 0:
        return 'negative'
    return 'zero'

Is it better to write:

def example(x):
    if x > 0:
        return 'positive'
    elif x < 0:
        return 'negative'
    else:
        return 'zero'

Both have the same outcome, but is one more efficient or considered more idiomatic than the other?

Edit:

A couple of people have said that in the first example both if statements are always evaluated, which doesn't seem to be the case to me

for example if I run the code:

l = [1,2,3]

def test(a):
    if a > 0:
        return a
    if a > 2:
        l.append(4)

test(5)

l will still equal [1,2,3]

解决方案

I'll expand out my comment to an answer.

In the case that all cases return, these are indeed equivalent. What becomes important in choosing between them is then what is more readable.

Your latter example uses the elif structure to explicitly state that the cases are mutually exclusive, rather than relying on the fact they are implicitly from the returns. This makes that information more obvious, and therefore the code easier to read, and less prone to errors.

Say, for example, someone decides there is another case:

def example(x):
    if x > 0:
        return 'positive'
    if x == -15:
        print("special case!")
    if x < 0:
        return 'negative'
    return 'zero'

Suddenly, there is a potential bug if the user intended that case to be mutually exclusive (obviously, this doesn't make much sense given the example, but potentially could in a more realistic case). This ambiguity is removed if elifs are used and the behaviour is made visible to the person adding code at the level they are likely to be looking at when they add it.

If I were to come across your first code example, I would probably assume that the choice to use ifs rather than elifs implied the cases were not mutually exclusive, and so things like changing the value of x might be used to change which ifs execute (obviously in this case the intention is obvious and mutually exclusive, but again, we are talking about less obvious cases - and consistency is good, so even in a simple example when it is obvious, it's best to stick to one way).

这篇关于何时在Python中使用if vs elif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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