<tfoot id='AUhOo'></tfoot>
  • <legend id='AUhOo'><style id='AUhOo'><dir id='AUhOo'><q id='AUhOo'></q></dir></style></legend>
    <i id='AUhOo'><tr id='AUhOo'><dt id='AUhOo'><q id='AUhOo'><span id='AUhOo'><b id='AUhOo'><form id='AUhOo'><ins id='AUhOo'></ins><ul id='AUhOo'></ul><sub id='AUhOo'></sub></form><legend id='AUhOo'></legend><bdo id='AUhOo'><pre id='AUhOo'><center id='AUhOo'></center></pre></bdo></b><th id='AUhOo'></th></span></q></dt></tr></i><div id='AUhOo'><tfoot id='AUhOo'></tfoot><dl id='AUhOo'><fieldset id='AUhOo'></fieldset></dl></div>

      • <bdo id='AUhOo'></bdo><ul id='AUhOo'></ul>
    1. <small id='AUhOo'></small><noframes id='AUhOo'>

      1. return 语句的目的是什么?它与印刷有何不同?

        What is the purpose of the return statement? How is it different from printing?(return 语句的目的是什么?它与印刷有何不同?)
        • <bdo id='r8xts'></bdo><ul id='r8xts'></ul>

          <legend id='r8xts'><style id='r8xts'><dir id='r8xts'><q id='r8xts'></q></dir></style></legend>

              <tbody id='r8xts'></tbody>

                1. <i id='r8xts'><tr id='r8xts'><dt id='r8xts'><q id='r8xts'><span id='r8xts'><b id='r8xts'><form id='r8xts'><ins id='r8xts'></ins><ul id='r8xts'></ul><sub id='r8xts'></sub></form><legend id='r8xts'></legend><bdo id='r8xts'><pre id='r8xts'><center id='r8xts'></center></pre></bdo></b><th id='r8xts'></th></span></q></dt></tr></i><div id='r8xts'><tfoot id='r8xts'></tfoot><dl id='r8xts'><fieldset id='r8xts'></fieldset></dl></div>
                  <tfoot id='r8xts'></tfoot>
                2. <small id='r8xts'></small><noframes id='r8xts'>

                  本文介绍了return 语句的目的是什么?它与印刷有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  什么是return语句的简单基本解释,在Python中如何使用?

                  What is the simple basic explanation of what the return statement is, how to use it in Python?

                  它和print语句有什么区别?

                  推荐答案

                  print() 函数在控制台中写入,即打印"一个字符串.return 语句使你的函数退出并将一个值返回给它的调用者.一般来说,函数的重点是接受输入并返回一些东西.return 语句在函数准备好向其调用者返回值时使用.

                  The print() function writes, i.e., "prints", a string in the console. The return statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The return statement is used when a function is ready to return a value to its caller.

                  例如,这是一个同时使用 print()return 的函数:

                  For example, here's a function utilizing both print() and return:

                  def foo():
                      print("hello from inside of foo")
                      return 1
                  

                  现在您可以运行调用 foo 的代码,如下所示:

                  Now you can run code that calls foo, like so:

                  if __name__ == '__main__':
                      print("going to call foo")
                      x = foo()
                      print("called foo")
                      print("foo returned " + str(x))
                  

                  如果您将其作为脚本(例如 .py 文件)而不是在 Python 解释器中运行,您将获得以下输出:

                  If you run this as a script (e.g. a .py file) as opposed to in the Python interpreter, you will get the following output:

                  going to call foo
                  hello from inside foo
                  called foo   
                  foo returned 1
                  

                  我希望这会让它更清楚.解释器将返回值写入控制台,这样我就能明白为什么有人会感到困惑.

                  I hope this makes it clearer. The interpreter writes return values to the console so I can see why somebody could be confused.

                  下面是来自解释器的另一个例子,它证明了这一点:

                  Here's another example from the interpreter that demonstrates that:

                  >>> def foo():
                  ...     print("hello from within foo")
                  ...     return 1
                  ...
                  >>> foo()
                  hello from within foo
                  1
                  >>> def bar():
                  ...   return 10 * foo()
                  ...
                  >>> bar()
                  hello from within foo
                  10
                  

                  可以看到,当 bar() 调用 foo() 时,1 并没有写入控制台.相反,它用于计算从 bar() 返回的值.

                  You can see that when foo() is called from bar(), 1 isn't written to the console. Instead it is used to calculate the value returned from bar().

                  print() 是一个会导致副作用的函数(它在控制台中写入一个字符串),但会从下一条语句继续执行.return 使函数停止执行并将值返回给调用它的任何对象.

                  print() is a function that causes a side effect (it writes a string in the console), but execution resumes with the next statement. return causes the function to stop executing and hand a value back to whatever called it.

                  这篇关于return 语句的目的是什么?它与印刷有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                3. <small id='lJJBI'></small><noframes id='lJJBI'>

                    <tfoot id='lJJBI'></tfoot>
                        <i id='lJJBI'><tr id='lJJBI'><dt id='lJJBI'><q id='lJJBI'><span id='lJJBI'><b id='lJJBI'><form id='lJJBI'><ins id='lJJBI'></ins><ul id='lJJBI'></ul><sub id='lJJBI'></sub></form><legend id='lJJBI'></legend><bdo id='lJJBI'><pre id='lJJBI'><center id='lJJBI'></center></pre></bdo></b><th id='lJJBI'></th></span></q></dt></tr></i><div id='lJJBI'><tfoot id='lJJBI'></tfoot><dl id='lJJBI'><fieldset id='lJJBI'></fieldset></dl></div>
                        <legend id='lJJBI'><style id='lJJBI'><dir id='lJJBI'><q id='lJJBI'></q></dir></style></legend>
                          <tbody id='lJJBI'></tbody>
                        • <bdo id='lJJBI'></bdo><ul id='lJJBI'></ul>