We can use lambda functions in Python with default arguments using the same syntax as regular functions. Here's an example:
main.py33 chars2 lines
In the code above, we have a lambda function that takes two arguments x
and y
with a default value of 5
. The lambda function simply returns the sum of x
and y
.
We can now call my_lambda
and pass in one or two arguments:
main.py97 chars3 lines
In the first call, we only passed in one argument (3
), so y
defaults to 5
and the result is 8
. In the second call, we passed in two arguments (3
and 7
), so y
is set to 7
and the result is 10
.
gistlibby LogSnag