How do I understand Python return?

Source: Internet
Author: User
For example, defadd (a, B) in learnpythonthehardway: printADDING % d + % d % (a, B) returna + B, for example, in learn python the hard way

def add(a, b):    print "ADDING %d + %d" % (a, b)    return a + b
Reply content: Read @ Zhang Xiaofeng's answer first. If you cannot understand this answer --

First, you need to know, what is a function?
The book may say that a function is a module that completes a function. To put it bluntly, the function is a worker you recruit.
You give him some materials, tell him how to assemble them, and then he is responsible for handing over the assembled finished products to you. The material is the parameter of the function, the finished product is the output of the function, and how to assemble it is the code of the function body you wrote.
For example, this code

def worker(a, b, c):    x = a + b    y = x * c
The return Statement returns the result to the called place and returns the control of the program together.
For example

num = add(a, b)
It seems that I was invited. I had a pitfall first. I started to write the answer. I wrote the answer later and stuck it. I got a thumbs up! To fix it!
I accidentally saw this issue. Write down my SUPERFICIAL OPINIONS ..
1, Function(Function) is a process) Parameters(Argument) to obtain Return Value(Return)
1.1 as you can understand above, the function is a process, the parameter is a raw material, and the return value is a finished product.
Answer:
"What operations does return a + B perform here ?"
Apparently, the operation a + B is executed, and a + B is returned to the add function, so that a + B becomes the processing result of the add function.
"What does return mean and how to use it ?"
Several elements of a function include: function name, parameter, processing process, and return result (return). The processing process is unnecessary, but the other three elements are certain (but return does not have to be written, if you do not specify return when defining a function, the function returns None by default. In this example, the function name add, parameters a and B are returned, and the result a + B is returned. For the meaning of return, refer to Qiu Ju's lawsuit: "I just want a saying (result)" or refer to the function to understand it.
As for how to use the function (taking your add function as an example), the following points are briefly summarized:
Normal version: return a + B
Manual version: returna + B # error NameError
Mentally retarded version: return a # Only returns
Mentally retarded version 2: return c + d # error NameError
Willful version: return 2 # That's exactly 2
Willful version 2: return a-B # You are happy :)
Empty version: return # returns an Noneprint, which simply outputs the structure to your display device without any other meaning.
Example of 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
Return parts_dict
Why is return? If you do not return, the dictionary you created will be down and the result will not be accessible after the function is run, because you only print the result to the output device. If you return this result, the result will be retained. You can also do other things based on this result, such:
My_auto_parts = autoparts ()
Print my_auto_parts ['engine']
When we call the function autoparts, it gives us the return value. We can store the return value in the variable my_auto_parts, after running the autoparts function, we can still use the my_auto_parts variable to obtain the dictionary content. Finally, we can output the target of the keyword "engine" in the dictionary. First, you need to call a function when defining it.
Sum = add (3, 4)
Output
Sum = 7
It can be seen that return returns the result of the following expression to sum
The key point is to define the function and then use the function. This is two steps. Ah, haha! The first professional question I answered in zhihu!

The function that returns your def explains this concept using the Chinese "return". All things are nonsense. If someone asks how to understand it, can you translate it into meaningful English?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.