akshay sharma
akshay sharma

Reputation: 157

Lambda in return statement gives two outputs but only one if print is not used

def x(n):
    return lambda a: print(a)
print(x(1)(2))

this outputs: 2 None

What is this None for? I don't understand the flow here...

Upvotes: 2

Views: 66

Answers (1)

Austin
Austin

Reputation: 26037

What you need is:

def x(n):
    return lambda a: a
print(x(1)(2))

When you do return lambda a: print(a), you print a as well as return what print() returns which you print when you call the function. print() statement returns a None. See:

print(print(1))
# 1
# None

Upvotes: 4

Related Questions