简体   繁体   中英

what's the actual difference between print and return

Here is my code. I would like to the the value for each key from python dict. So when I use print in function it's working fine but when I use return in function it's just return any one value not all. So, how can I get all values using return() ?

def tech(arg):
    for te in arg.values():
        return(te)
print(tech({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
  'Kenneth Love': ['Python Basics', 'Python Collections']}))

return is a statement ; a syntactic construct. It returns control - and an optional value - to the caller of the function. In other words, it terminates a function. That's why only one item was returned from tech ; After the first iteration of the for loop, you immediately returned control, and the value of te , to the point where tech was called.

print on the other hand is a function . It will simply prints its argument to 1 standard output. In your case, you looped through all of the values in arg.values() and printed them via print . Once done, your function implicitly returned None and ended.

What you seem to want is a generator . A generator is basically a function that can return multiple values. You use the yield keyword to do this:

>>> def gen():
    for n in [1, 2, 3]:
        yield n


>>> # gen returns a generator object. We can call this objects

>>> # __next__ method via next() to retrieve the next value from it.
>>> gen_obj = gen()
>>> next(gen_obj)
1
>>> next(gen_obj)
2
>>> next(gen_obj)
3
>>> next(gen_obj)
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    next(gen_obj)
StopIteration
>>> 

Just for the sake of completeness, here is how you would use yield in your example code:

def tech(arg):
    for te in arg.values():
        yield te

print(list(tech({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
  'Kenneth Love': ['Python Basics', 'Python Collections']})))

EDIT: Since you appear to be using Python 3.3 or greater, you can use the yield from syntax.This will allow you to convert the generator, to a flat list:

def tech(arg):
    for te in arg.values():
        yield from te

print(list(tech({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
  'Kenneth Love': ['Python Basics', 'Python Collections']})))

1 Technically, print can also write to other places given the correct keyword arguments. But I avoid mentioning this as this would be distracting from my main points.

If you call the function from somewhere else, like another function or just from the terminal, a return statement would let you store the return value in a variable. Print simply displays the input given in the print function to the terminal where you call your tech-function

return terminates (ends) the function it's in, and wherever it was called from receives the value that it returned.

So in your code: The loop starts, it hits the return statement, so it ends the function (and ends the loop) and just returns the first element it was at. Then the print statement, in which you called your function, will just print that one element, and that's it.

On the other hand, if you replace your return with print , then the function doesn't get terminated prematurely, so the loop runs over all the values and prints them.


What you seem to have in mind, somewhat, is turning your function into an iterator. Try replacing return with yield and see what happens, then read up on iterators and yield in the python documentation!

Because when reached, return statement will terminate the function. To get the behavior you want, just rewrite it as

def tech(arg):
  return arg.values():

Your code consist of two main blocks: 1. define a function 'tech' 2. print the result of a call to function 'tech'

Inside function tech there is a 'for te in arg.values()' loop. The loop gets initialized with the first value and the loop encounters the 'return' statement which forces the current value to be returned from function 'tech'. The loop is aborted

The print function receives the result of function tech and prints it (containing only the first argument)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM