[issue40641] Reserved word pair used in lambda tutorial without being noted as a reserved word
New submission from Chas Belov <docorbit@sonic.net>: In the tutorial for lambda expressions at https://docs.python.org/3.7/tutorial/controlflow.html#lambda-expressions the reserved word pair is introduced without noting that it is a reserved word. In the example given, I wasn't sure whether pair was a reserved word or whether the interpreter was parsing the plural "pairs" which is presumable an arbitrary name. Actual content: The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument:
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')]
Candidate expected content: The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument, for example, the reserved word pair to designate the position in a tuple pair:
items = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] items.sort(key=lambda pair: pair[1]) items [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
---------- assignee: docs@python components: Documentation messages: 369017 nosy: docorbit@sonic.net, docs@python priority: normal severity: normal status: open title: Reserved word pair used in lambda tutorial without being noted as a reserved word versions: Python 3.7 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40641> _______________________________________
Mark Dickinson <dickinsm@gmail.com> added the comment: `pair` isn't a reserved word here; it's just the formal parameter name. Think of the lambda here as being equivalent to a function def second(pair): return pair[1] You can replace the word "pair" here with any other valid Python identifier, and it'll still work the same way. ---------- nosy: +mark.dickinson _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40641> _______________________________________
Change by Mark Dickinson <dickinsm@gmail.com>: ---------- resolution: -> not a bug stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40641> _______________________________________
participants (2)
-
Chas Belov -
Mark Dickinson