Python Lambda: a small function inline
lambda args: expression makes an unnamed function whose body is one expression. It can be passed as the key argument of list.sort or sorted.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-74-A: sorted with a lambda key, and a lambda assigned to a name
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
words = ['pear', 'fig', 'banana']
print(sorted(words, key=lambda w: len(w)))
add = lambda a, b: a + b
print(add(2, 3))
EOSCommand:
python3 s.pyRecorded output:
['fig', 'pear', 'banana']
5Exit status: 0. The output matches the expected text written before the run.
TE-74-B: a lambda can be called on the spot, and map applies one to every item
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
print((lambda x: x * 2)(4))
print(list(map(lambda x: x + 1, [1, 2])))
EOSCommand:
python3 s.pyRecorded output:
8
[2, 3]Exit status: 0. This status and the output match what was written before the run.
TE-74-C: expected non-zero exit: calling a two-argument lambda with one argument raises TypeError
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
f = lambda a, b: a + b
f(1)
EOSCommand:
python3 s.pyRecorded output: none, standard output was empty.
Standard error from the same run:
Traceback (most recent call last):
File "/tmp/b21-rqhcf5kl/work/s.py", line 2, in <module>
f(1)
TypeError: <lambda>() missing 1 required positional argument: 'b'Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
TE-74-B: the lambda called with 4 returned 8, and map with a lambda that adds 1 gave [2, 3]. TE-74-C: calling the two-argument lambda with one argument stopped with exit status 1. The last line of standard error is TypeError: <lambda>() missing 1 required positional argument: 'b'.
Limits
A lambda holds one expression and no statements. Late binding of loop variables was not tested.
Source
- https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions
- https://docs.python.org/3/library/functions.html#sorted
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-74-A | sorted with a lambda key, and a lambda assigned to a name | 0 | PASS | 3d5f33c83061 | | TE-74-B | a lambda can be called on the spot, and map applies one to every item | 0 | PASS | e878a34df06d | | TE-74-C | expected non-zero exit: calling a two-argument lambda with one argument raises TypeError | 1 | PASS | 5f0eb4e8ef4d |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides