在函数中使用两个 return 语句(Python 2.7) [英] Using two return statements in a function (Python 2.7)

查看:73
本文介绍了在函数中使用两个 return 语句(Python 2.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个函数的输出感到困惑:

I am getting confused with the output of this function:

def doPrint():
    print "Hello",
    return "World"
    print "Goodbye",
    return "World"

print doPrint()

输出是Hello World".那么,有人能解释一下为什么它没有打印出再见世界"吗?

The output is "Hello World". So, can someone explain why it didn't print out "Goodbye World"?

推荐答案

该函数只打印出Hello".

The function only prints out "Hello ".

然后它返回,它的返回值为World.".返回意味着函数完成,解释器在调用函数之前的任何地方继续,所以返回之后的任何内容都无关紧要.

Then it returns, and its return value is "World.". Returning means the function is finished and the interpreter continues wherever it was before the function was called, so whatever comes after the return is irrelevant.

您将其称为 print doPrint(),它调用函数并打印返回值(我们知道结果是World").

You called it as print doPrint(), which calls the function and prints whatever the return value is (we know it turned out to be "World.").

所以最终结果是Hello World".已打印.

So the end result is that "Hello World." was printed.

这篇关于在函数中使用两个 return 语句(Python 2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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