简体   繁体   English

Python if():vs if:

[英]Python if( ): vs if:

On Code Academy there is this course where in the example they show 在Code Academy上的这门课程中,他们展示了示例

def speak(message):
    return message

if happy():
    speak("I'm happy!")
elif sad():
    speak("I'm sad.")
else:
    speak("I don't know what I'm feeling.")

The above example will NOT be related to the rest of the code I show. 上面的例子将不会涉及到我显示代码的其余部分。 That was just an example for the if statement. 那只是if语句的一个例子。 Now I was under the impression that when ever writing an if statement it had to end in an (): like the above example. 现在,我的印象是,无论何时编写if语句,它都必须以():结尾():就像上面的示例一样。

However when doing the assignments this does not work: 但是,在进行分配时,这将不起作用:

def shut_down(s):
    if s == "yes"():
        return "Shutting down"
    elif s == "no"():
        return "Shutdown aborted"
    else:
        return "Sorry"

However this works: 但这有效:

def shut_down(s):
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:
        return "Sorry"

My question is how come the () is not needed next to the "yes" and "no " but : is still needed. 我的问题是怎么来的()是不是旁边的需要"yes""no ”,而是:仍然是必要的。 I thought whenever writing an if statement it will automatically have to end with (): . 我认为无论何时编写if语句,它都必须自动以():结尾。 In that very first example, that's how it is shown. 在第一个示例中,就是这样显示的。 Do you understand my confusion. 你明白我的困惑吗?

In the example given, happy() and sad() are functions, and as such require parentheses. 在给出的示例中, happy()sad()是函数,因此需要括号。 The if itself does not need parentheses at the end (and it shouldn't have them) if本身不需要最后加括号(并且不应该加括号)

No, if has nothing to do with () 否, if()无关

happy is a function. happy是一种功能。 happy() is a call to that function. happy()是对该函数的调用。 So, if happy(): tests if the happy function returns true when called. 因此, if happy():测试happy函数在调用时是否返回true。

In other words, if happy(): speak("I'm happy!") is equivalent to 换句话说, if happy(): speak("I'm happy!")等效于

result_of_happy = happy()
if result_of_happy:
    speak("I'm happy!")

As has been mentioned happy() / sad() are functions so they require () . 如前所述, happy() / sad()是函数,因此它们需要() In example two of your question you are comparing your value to the string "yes" because it is a string it does not require () . 在问题的示例二中,您将您的值与字符串"yes"进行比较,因为它是不需要()的字符串。

Within an if statement you can use parentheses to make the code more readable and ensure certain operations are evaluated before others. if语句中,可以使用括号使代码更具可读性,并确保某些操作先于其他操作进行评估。

if (1+1)*2 == 4:
    print 'here'
else:
    print 'there'

Differs from: 不同于:

if 1+1*2 == 4:
    print 'here'
else:
    print 'there'

Because string objects are not callable so what are you expecting then: 因为字符串对象不可调用,所以您期望什么:

Then use lambda not that efficient tho: 然后使用lambda不是那么有效的方法:

def shut_down(s):
    if (lambda: s == "yes")():
        return "Shutting down"
    elif (lambda: s == "no")():
        return "Shutdown aborted"
    else:
        return "Sorry"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM