Reputation: 39
My question is for below code why None is getting print in place of function output and why function output is getting before its position.
def print_spam():
print('spam')
def do_twice(r,ps):
g = ps()
print(r,'is a',g)
print(r,'is a',ps())
do_twice('xyz',print_spam)
Output is
spam
xyz is a None
spam
xyz is a None
Upvotes: 2
Views: 198
Reputation: 5534
Consider replacing
g = ps()
print(r,'is a',g)
with
g = ps
print r,'is a',
g()
anyway, as pointed out by some other answers, print_spam() returns None
Upvotes: 1
Reputation: 59984
The function print_spam()
does not return
anything. It just print
s a statement.
Change it to:
def print_spam():
print('spam')
return 'spam'
Because your function doesn't return anything, it defaults to None
. Now when assigning the function output to g
, it will contain the returned string of the function (spam
).
Upvotes: 11