In neither case you are passing a function that takes arguments. In second case the value of v
is bound to the integer 2
, whereas in the first case v
refers to a callable anonymous lambda function () -> sum + a
.
The following 2 are almost equivalent:
def x():
return sum + a
x = lambda: sum + a
except lambda does not provide nice debugging traceback, since lambda functions are unnamed.
Also, the names of the variables are a bit misleading, took me a some moments to figure out what is happening. There are exactly 2 variables called sum
- the one within accum
function, and the other in the global scope. There is also the 3rd, masked one that contains the global built-in function sum
...