Jul-16-2019, 04:54 AM
(This post was last modified: Jul-16-2019, 04:54 AM by Gribouillis.)
After the two first lines, x is no longer an integer, it is an anonymous function
>>> x = 10 >>> x = lambda: x + 2 >>> x <function <lambda> at 0x7fb32e167048>At this stage,
x + 2
has not yet been executed. It's only executed when you call x()
but then it is an error because you're trying to add a function and an integer>>> x() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <lambda> TypeError: unsupported operand type(s) for +: 'function' and 'int'See this other example which is a regular function but works the same
>>> y = 122 >>> def spam(): ... print('The value of y is', y) ... >>> y = 314 >>> spam() The value of y is 314