tags:

views:

2705

answers:

4

Hi again,

I've made a function that creates a dictionary. In my previous thread/question, Andrew Jaffe writes this:

"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."

With the help from this forum, I now have this function:

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

This function creates a dictionary, but it does not return something. I could have added print parts_dict, and it would have returned the dictionary. I dont see the difference between the return statement and that the function prints out the dictionary. Can someone please explain this to me?

+2  A: 

you just add 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
  return parts_dict

printing out only prints out to the standard output (screen) of the application. You can also return multiple things by separating them with commas:

return parts_dict, list_of_parts

to use it:

test_dict = {}
test_dict = autoparts()
jle
+7  A: 

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.

ryeguy
Thank you for a good answere!
Thanx
Good answer. For tutorial-type answers, try to use good Python (PEP 8) coding style; in this case, CamelCase (MyAutoPartsList) should be reserved for class names; a simple variable like that should be my_auto_parts_list, or my_auto_parts (it's not a list!), or just auto_parts.
Carl Meyer
Woops, thanks for that. It's been awhile since I did python.
ryeguy
+2  A: 

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)

Jason Baker
+1  A: 

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.

RossFabricant