Lets say I define a simple function which will display an integer passed to it:
def funct1(param1):
print(param1)
return(param1)
the output will be the same but and I know that when a return开发者_开发问答
statement is used in a function the output can be used again. Otherwise the value of a print
statement cannot be used. But I know this is not the formal definition, Can anyone provide me with a good definition?
Dramatically different things. Imagine if I have this python program:
#!/usr/bin/env python
def printAndReturnNothing():
x = "hello"
print(x)
def printAndReturn():
x = "hello"
print(x)
return x
def main():
ret = printAndReturn()
other = printAndReturnNothing()
print("ret is: %s" % ret)
print("other is: %s" % other)
if __name__ == "__main__":
main()
What do you expect to be the output?
hello
hello
ret is : hello
other is: None
Why?
Why? Because print
takes its arguments/expressions and dumps them to standard output, so in the functions I made up, print
will output the value of x
, which is hello
.
printAndReturn
will returnx
to the caller of the method, so:ret = printAndReturn()
ret
will have the same value as x
, i.e. "hello"
printAndReturnNothing
doesn't return anything, so:other = printAndReturnNothing()
other
actually becomes None
because that is the default return from a python function. Python functions always return something, but if no return
is declared, the function will return None
.
Resources
Going through the python tutorial will introduce you to these concepts: http://docs.python.org/tutorial
Here's something about functions form python's tutorial: http://docs.python.org/tutorial/controlflow.html#defining-functions
This example, as usual, demonstrates some new Python features:
The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.
With print()
you will display to standard output the value of param1
, while with return
you will send param1
to the caller.
The two statements have a very different meaning, and you should not see the same behaviour. Post your whole program and it'll be easier to point out the difference to you.
Edit: as pointed by others, if you are in an interactive python shell you see the same effect (the value is printed), but that happens because the shell evaluates expressions and prints their output.
In this case, a function with a return
statement is evaluated as the parameter of return
itself, so the return value is echoed back. Don't let the interactive shell fool you! :)
Simple example to show the difference:
def foo():
print (5)
def bar():
return 7
x = foo()
y = bar()
print (x)
# will show "None" because foo() does not return a value
print (y)
# will show "7" because "7" was output from the bar() function by the return statement.
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:
def function_that_prints():
print "I printed"
def function_that_returns():
return "I returned"
f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2
print
(or print()
if you're using Python 3) does exactly that—print anything that follows the keyword. It will also do nice things like automatically join multiple values with a space:
print 1, '2', 'three'
# 1 2 three
Otherwise print
(print()
) will do nothing from your program's point of view. It will not affect the control flow in any way and execution will resume with the very next instruction in your code block:
def foo():
print 'hello'
print 'again'
print 'and again'
On the other hand return
(not return()
) is designed to immediately break the control flow and exit the current function and return a specified value to the caller that called your function. It will always do this and just this. return
itself will not cause anything to get printed to the screen. Even if you don't specify a return value an implicit None
will get returned. If you skip a return
altogether, an implicit return None
will still happen at the end of your function:
def foo(y):
print 'hello'
return y + 1
print 'this place in code will never get reached :('
print foo(5)
# hello
# 6
def bar():
return # implicit return None
print bar() is None
# True
def baz(y):
x = y * 2
# implicit return None
z = baz()
print z is None
# True
The reason you see return
ed values printed to the screen is because you are probably working in the interactive Python shell that automatically print
s any result for your own convenience.
The output is only the same in the interactive terminal. When you execute your program normally the results will be completely different.
I would suggest you to read a book about Python or read a tutorial that teaches you the basics, because this is a very basic thing.
精彩评论