返回函数的输出与打印它有何不同?
除了所有其他提示和技巧之外,我认为您还缺少一些关键的东西:您的函数实际上需要返回一些东西。 当您创建
autoparts()
或splittext()
时,我们的想法是,这将是您可以调用的函数,并且它可以(并且应该)返回一些内容。 一旦计算出您希望函数具有的输出,您需要将其放入return
语句中。
def autoparts():
parts_dict = {}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
k, v = line.split()
parts_dict[k] = v
print(parts_dict)
>>> autoparts()
{'part A': 1, 'part B': 2, ...}
该函数创建一个字典,但不返回任何内容。 但是,由于我添加了 print
,因此当我运行该函数时会显示该函数的输出。 返回
和打印
有什么区别?
In my previous question, Andrew Jaffe writes:
In addition to all of the other hints and tips, I think you're missing something crucial: your functions actually need to return something.
When you createautoparts()
orsplittext()
, the idea is that this will be a function that you can call, and it can (and should) give something back.
Once you figure out the output that you want your function to have, you need to put it in areturn
statement.
def autoparts():
parts_dict = {}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
k, v = line.split()
parts_dict[k] = v
print(parts_dict)
>>> autoparts()
{'part A': 1, 'part B': 2, ...}
This function creates a dictionary, but it does not return something. However, since I added the print
, the output of the function is shown when I run the function. What is the difference between return
ing something and print
ing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
print
只是将结构打印到输出设备(通常是控制台)。 而已。 要从你的函数返回它,你会这样做:为什么返回? 如果不这样做,该字典就会消失(被垃圾收集),并且一旦该函数调用结束就无法再访问。 如果返回该值,则可以用它做其他事情。 如:
看看发生了什么? 调用
autoparts()
并返回parts_dict
,我们将其存储到my_auto_parts
变量中。 现在我们可以使用这个变量来访问字典对象,即使函数调用结束,它仍然继续存在。 然后我们用'engine'
键打印出字典中的对象。如需优秀教程,请查看深入了解 python。 它是免费的并且非常容易遵循。
print
simply prints out the structure to your output device (normally the console). Nothing more. To return it from your function, you would do:Why return? Well if you don't, that dictionary dies (gets garbage collected) and is no longer accessible as soon as this function call ends. If you return the value, you can do other stuff with it. Such as:
See what happened?
autoparts()
was called and it returned theparts_dict
and we stored it into themy_auto_parts
variable. Now we can use this variable to access the dictionary object and it continues to live even though the function call is over. We then printed out the object in the dictionary with the key'engine'
.For a good tutorial, check out dive into python. It's free and very easy to follow.
print 语句将向用户输出一个对象。 return 语句将允许在函数完成后将字典分配给变量。。
或者在返回字典的情况下:(
执行一行后没有打印任何内容的语句意味着最后一条语句返回 None)
The print statement will output an object to the user. A return statement will allow assigning the dictionary to a variable once the function is finished.
Or in the context of returning a dictionary:
(The statements where nothing is printed out after a line is executed means the last statement returned None)
我认为您很困惑,因为您是从 REPL 运行的,当您调用函数时,它会自动打印出返回的值。 在这种情况下,无论您有一个创建值、打印该值并将其丢弃的函数,还是有一个创建值并返回该值并让 REPL 打印它的函数,您都会得到相同的输出。
然而,这些并不是一回事,当您使用另一个想要对 autoparts 创建的值执行某些操作的函数调用 autoparts 时,您就会意识到这一点。
I think you're confused because you're running from the REPL, which automatically prints out the value returned when you call a function. In that case, you do get identical output whether you have a function that creates a value, prints it, and throws it away, or you have a function that creates a value and returns it, letting the REPL print it.
However, these are very much not the same thing, as you will realize when you call autoparts with another function that wants to do something with the value that autoparts creates.
您只需添加一个返回语句...
打印输出仅打印到应用程序的标准输出(屏幕)。 您还可以通过用逗号分隔来返回多个内容:
使用它:
you just add a return statement...
printing out only prints out to the standard output (screen) of the application. You can also return multiple things by separating them with commas:
to use it:
主要区别:
调用print将立即让您的程序写出文本供您查看。 当您想向人们展示价值时,请使用印刷品。
return 是一个关键字。 当到达return语句时,Python将停止当前函数的执行,并将一个值发送到调用该函数的位置。 当您想要将值从代码中的一个点发送到另一点时,请使用 return。
使用 return 会更改程序的流程。 使用打印则不会。
Major difference:
Calling print will immediately make your program write out text for you to see. Use print when you want to show a value to a human.
return is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.
Using return changes the flow of the program. Using print does not.
从根本上来说,函数是一个可以执行的代码块,不是在编写时执行,而是在调用时执行。 假设我有以下一段代码,它是一个简单的乘法函数:
因此,如果我使用
multiply(2,3)
调用该函数,它将返回值 6。如果我修改了函数看起来像这样:...然后输出如您所料,打印数字 6。 然而,这两个语句之间的区别在于,
print
仅在控制台上显示一些内容,而return
“返回一些内容”给调用它的任何东西,这通常是一个变量。 然后,该变量被赋予它所调用的函数中的 return 语句的值。 下面是 python shell 中的一个示例:现在函数已将调用函数的结果返回到调用它的位置,在本例中是一个名为“answer”的变量。
这不仅仅是打印结果,因为您可以再次访问它。 这是使用 return 语句的函数的示例:
因此它基本上将调用函数的结果存储在变量中。
A function is, at a basic level, a block of code that can executed, not when written, but when called. So let's say I have the following piece of code, which is a simple multiplication function:
So if I called the function with
multiply(2,3)
, it would return the value 6. If I modified the function so it looks like this:...then the output is as you would expect, the number 6 printed. However, the difference between these two statements is that
print
merely shows something on the console, butreturn
"gives something back" to whatever called it, which is often a variable. The variable is then assigned the value of the return statement in the function that it called. Here is an example in the python shell:So now the function has returned the result of calling the function to the place where it was called from, which is a variable called 'answer' in this case.
This does much more than simply printing the result, because you can then access it again. Here is an example of the function using return statements:
So it basically stores the result of calling a function in a variable.
这样它就可以成为一个变量。
但是,如果“add”函数打印输出“sum”,则输出“sum”将为 None,因为操作在分配后就已经发生了。
That way it can then become a variable.
But if the 'add' function print the output 'sum' would then be None as action would have already taken place after it being assigned.
不幸的是,有字符限制,所以这将在很多部分。 首先要注意的是 return 和 print 是语句,而不是函数,但这只是语义。
我将从基本解释开始。 print 只是向人类用户显示一个表示计算机内部正在发生的事情的字符串。 计算机无法使用该打印。 return 是函数返回值的方式。 该值通常是人类用户看不到的,但计算机可以在其他功能中使用它。
更广泛地说,打印不会以任何方式影响功能。 它只是为了人类用户的利益而存在。 它对于理解程序如何工作非常有用,并且可以在调试中用于在不中断程序的情况下检查程序中的各种值。
return 是函数返回值的主要方式。 所有函数都会返回一个值,如果没有 return 语句(或yield,但不用担心),它将返回 None。 然后,函数返回的值可以进一步用作传递给另一个函数的参数、存储为变量,或者只是为了人类用户的利益而打印。
考虑这两个程序:
print "Now let us see the value of f1 and f2 are"
print f1 --->None
print f2---->"I returned"
当 function_that_prints 运行时,它自动打印到控制台“我打印了”。 然而,存储在 f1 中的值为 None,因为该函数没有 return 语句。
当 function_that_returns 运行时,它没有向控制台打印任何内容。 然而,它确实返回了一个值,并且该值存储在 f2 中。 当我们在代码末尾打印 f2 时,我们看到“I returned”
Unfortunately, there is a character limit so this will be in many parts. First thing to note is that return and print are statements, not functions, but that is just semantics.
I’ll start with a basic explanation. print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.
On a more expansive note, print will not in any way affect a function. It is simply there for the human user’s benefit. It is very useful for understanding how a program works and can be used in debugging to check various values in a program without interrupting the program.
return is the main way that a function returns a value. All functions will return a value, and if there is no return statement (or yield but don’t worry about that yet), it will return None. The value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or just printed for the benefit of the human user.
Consider these two programs:
print "Now let us see what the values of f1 and f2 are"
print f1 --->None
print f2---->"I returned"
When function_that_prints ran, it automatically printed to the console "I printed". However, the value stored in f1 is None because that function had no return statement.
When function_that_returns ran, it did not print anything to the console. However, it did return a value, and that value was stored in f2. When we printed f2 at the end of the code, we saw "I returned"
下面的例子可能有助于理解:
The below examples might help understand: