Python:我是否必须使用else语句(除非代码强制要求)?

时间:2016-05-30 08:14:30

标签: python python-3.x if-statement

我了解ifelseelif语句。但是,我不确定它们在功能块中与return一起使用的最佳实践。使用返回块编写if / else结构的最Pythonic方法是什么?

# Other's code:
def my_function():
    do = input("Enter 'n' to print 1 and any other key to print 2")
    if do == "n":
        print(1)
        return
    else:
        print("2")
        return

# My code:
def my_function():
    do = input("Enter 'n' to print 1 and any other key to print 2")
    if do == "n":
        print(1)
        return
    # Note that I do not use an else statement here
    print("2")
    return

最后一行的else(或elif)语句是否需要 ?当我可以在else语句后输入我的代码时,我没有看到一个额外elif(或if)语句的重点。

我也很困惑,因为我最近了解到else语句可以与循环配对。 (例如for ... else ...

3 个答案:

答案 0 :(得分:1)

也许你需要一个真实代码的例子。让我们先使用elifelse的那个。

x = 0
if x == 0:
    print('x == 0')
    x = 1
elif x == 1:
    print('x == 1')
    x = 2
else:
    print('x is not 0 or 1')

这将打印x == 0

现在我们将看看您喜欢的解决方案(以及您认为相同的解决方案)。

x = 0
if x == 0:
    print('x == 0')
    x = 1
if x == 1:
    print('x == 1')
    x = 2
print('x is not 0 or 1')

这将打印x == 0x == 1x is not 0 or 1

elifelse的使用不是编程风格的问题,而是编程逻辑。

答案 1 :(得分:1)

您的代码不是elifelse的好例子。但我会首先分析你的例子。

  1. 第一个条件:
  2. select = input("Enter 'a' to add, 's' to subtract, 'x' to exit: ")
    if select == "a":
        # code
    elif select == "s": 
        # code
    elif select == "x": 
        return
    

    这里至少需要一个else。如果用户输入y,会发生什么?

    您的代码:

    if select == "a":
        # code
    if select == "s": 
        # code
    if select == "x": 
        return
    

    将始终检查3个if条件。在这种情况下,这不是效率。有时它会完全不同的情况。

    我将使用whileelse编写代码,以便:

    while(True)
        select = input("Enter 'a' to add, 's' to subtract, 'x' to exit: ")
        if select == "a":
            # code
            break
        elif select == "s": 
            # code
            break
        elif select == "x": 
            return
        else:
            print "Input is invalid!"
    
    1. 第二个条件是对的。这里可能没有必要else。但最好用else编写代码(它执行相同的任务,但更清楚):
    2. if do == "n":
          print(1)
      else:
          print("2")
      return
      

      P / S:所以你看到: else并非无用。始终尝试再次检查您的代码并找到改进它的方法。

答案 2 :(得分:0)

两个代码块不等同:

在第一个问题中,select只会进行一次测试,如果它是" a" (效率更高),如果print不是a,s或x,则select只会执行

select = input("Enter 'a' to add, 's' to subtract, 'x' to exit: ")
if select == "a":
    # code
elif select == "s": 
    # code
elif select == "x": 
    return
else:
    print("some text")

# *** This line is executed unless select is "x" ***

但是,在您的代码中,print 始终已执行,除非select为" x"。如果select是" a"再次测试" s"和" x"。在这种情况下,额外的测试可能不会是一个很大的开销,但如果select是一个复杂的对象,那么比较(涉及方法调用)可能很重要。

select = input("Enter 'a' to add, 's' to subtract, 'x' to exit: ")
if select == "a":
    # code
if select == "s": 
    # code
if select == "x": 
    return

# *** This line is executed unless select is "x" ***
print("some text")

除非您的代码中有多个return语句。从函数中的一个点开始只考虑return通常被认为是一种好习惯。原因是这使得代码更易于遵循和维护,并且更容易调试。

不要忘记"浪费"一行代码。额外的行不一定效率低,编译所花费的时间微不足道。在这种情况下,由于运行时测试较少,具有更多行的代码实际上更有效。 Python不像shell一样逐行解析和执行(例如bash)。您的代码易于维护更为重要。