lambda
lambda exapression is just a syntax suger for regular function definition.
func = lambda n: op(n)
could be translated to:
def func(n):
return op(n)
Packing exapression is legal in lambda expression.
func = lambda *args: op(args)
could be translate to:
def func(*args):
return op(args)
Example: sorting
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Example: default argument
>>> func = lambda a, b=3: (a, b)
>>> func(2)
(2, 3)
Example: unpacking operator
>>> out = lambda *x: list(map(str, x))
>>> out('abc', 124)
['abc', '124']
Note
As mentioned above,``lambda`` nothing but a syntax suger. So lambda could also accept *x as arguments, like regular function, when the number of arguments cannot be determined. Here x is unpacked to a tuple.