I just learned (am learning) how function parameters work in Python, and I started experimenting with it for no apparent reason, when this:
def jiskya(x, y):
if x > y:
print y
else:
print x
print(jiskya(2, 3))
gave the ouput:
>>>
2
None
Where did the None
come from? And what is it?
I see all your answers say the same. Take a look at this code:
And the output:
So I have used Return only. Why do I see the nasty none then?
The problem is you wrote
print jiskya(2,3)
. You're passing the return value ofjiskya
to theprint
function.jiskya
itself printsx
ory
, which is why you see the 2. But then theprint
in theprint jiskya(2, 3)
statement itself executes with no argument.To the interpreter, this is a simplification of what happens:
Ok, to start off when you do this:
You getting something pretty much equivalent to this:
So, what is going on? The
print(2)
is printing out 2, and returnsNone
which is printed by the outer call. Straightforward enough.Now look at this:
If you do:
You get 2 because if you print out a function you get whatever the
return
value is. (Thereturn
value is denoted by thereturn someVariable
.Now even though
print
doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when youprint print someVariable
, you will getNone
as the second part because the return value of print isNone
.So as others have stated:
Should be re-written:
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
Then,
The function.
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using
None
, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing,None
is replaced with the string "None".None
is a special object that is supposed to represent the absence of any real thing. Its type isNoneType
(it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.You wrote the function to print one of the two values
x
ory
, but not to return anything. SoNone
was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which wasNone
, as the text "None".