| name | use-lambda-inline |
| description | For inline callbacks: anonymous one-liner functions, key functions for sorting, trivial transformations as dict values. |
use-lambda-inline
When to Use
- Simple one-liner needed inline
key= argument to sorted/max/min
- Dict of operation functions
- Trivial transformations
- Callback registration
When NOT to Use
- Multiple statements needed
- Function will be reused by name
- Complex logic that needs documentation
- Recursion needed (lambda can't easily recurse)
The Pattern
Use lambda for anonymous, single-expression functions.
sorted(items, key=lambda x: x.priority)
max(scores, key=lambda s: s.value)
ops = {'+': lambda a, b: a + b,
'-': lambda a, b: a - b,
'*': lambda a, b: a * b}
result = ops['+'](3, 4)
positive = filter(lambda x: x > 0, numbers)
buttons = [Button(lambda i=i: click(i)) for i in range(10)]
Example (from pytudes)
env = {
'+': op.add,
'-': op.sub,
'apply': lambda proc, args: proc(*args),
'begin': lambda *x: x[-1],
'car': lambda x: x[0],
'cdr': lambda x: x[1:],
'cons': lambda x, y: [x] + y,
'length': len,
'list': lambda *x: list(x),
'map': lambda *args: list(map(*args)),
'null?': lambda x: x == [],
'number?': lambda x: isinstance(x, Number),
}
max(candidates, key=lambda c: probability(c))
s = min((s for s in squares if len(values[s]) > 1),
key=lambda s: len(values[s]))
sorted(items, key=lambda x: (x.category, -x.priority, x.name))
result = re.sub(r'[A-Z]+',
lambda m: translate_word(m.group()),
formula)
Key Principles
- One expression only: No statements, just return value
- Use
op module for operators: op.add clearer than lambda a,b: a+b
- Default args for capture:
lambda i=i: captures current i
- Keep it short: If complex, use named function
- Readable keys:
key=lambda x: x.name is clear