Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is returning the output of a function different from printing it?

Tags:

python

return

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 create autoparts() or splittext(), 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 a return 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 returning something and printing it?

like image 237
Thanx Avatar asked Apr 15 '09 02:04

Thanx


People also ask

What is the difference between printing and returning in Python?

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.

What is the difference between print and input function?

print function displays the given message on the screen. For example. print ("Hello") gives output as Hello Whereas input function accepts given data. For example, input = "Enter your age :" shows output as Enter your age and when you enter your age and press enter it takes in your age.

What happens when a function is returned?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

Does return also print?

The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function.


2 Answers

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.

>>> def foo(): ...     print "Hello, world!" ...  >>> a = foo() Hello, world! >>> a >>> def foo(): ...     return "Hello, world!" ...  >>> a = foo() >>> a 'Hello, world!' 

Or in the context of returning a dictionary:

>>> def foo(): ...     print {'a' : 1, 'b' : 2} ...  >>> a = foo() {'a': 1, 'b': 2} >>> a >>> def foo(): ...     return {'a' : 1, 'b' : 2} ...  >>> a = foo() >>> a {'a': 1, 'b': 2} 

(The statements where nothing is printed out after a line is executed means the last statement returned None)

like image 34
Jason Baker Avatar answered Oct 08 '22 15:10

Jason Baker


print simply prints out the structure to your output device (normally the console). Nothing more. To return it from your function, you would do:

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 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:

my_auto_parts = autoparts()  print(my_auto_parts['engine'])  

See what happened? autoparts() was called and it returned the parts_dict and we stored it into the my_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.

like image 61
ryeguy Avatar answered Oct 08 '22 16:10

ryeguy