@@ -6,22 +6,39 @@ is declared without a name. It is a small and restricted function that is no
66longer than one line. Like a normal function, a lambda function can have several
77arguments, but only one expression that is evaluated and returned.
88
9- The syntax of a lambda function is
10-
11- :samp: `lambda { ARGUMENTS } : { EXPRESSION } `
12-
13- .. code-block :: pycon
14-
15- >>> add = lambda x, y: x + y
16- >>> add(2, 3)
17- 5
9+ The syntax of a lambda function is :samp: `lambda { ARGUMENTS } : { EXPRESSION } `
1810
1911.. note ::
2012 There is no ``return `` statement in the lambda function. The single
2113 expression after the colon is the return value.
2214
23- In the next example, a lambda function is created within a function call.
24- However, there is no global variable to store the values of the lambda function:
15+ .. tip ::
16+ However, always use a ``def `` statement instead of an assignment that binds a
17+ lambda expression directly to an identifier – in other words, use
18+
19+ .. code-block :: pycon
20+ :class: good
21+
22+ >>> def add(x, y):
23+ ... return x + y
24+ ...
25+ >>> add(2, 3)
26+ 5
27+
28+ rather than
29+
30+ .. code-block :: pycon
31+ :class: bad
32+
33+ >>> add = lambda x, y: x + y
34+ >>> add(2, 3)
35+ 5
36+
37+ This is generally more useful for tracebacks and string representations. The
38+ only advantage of lambda functions is that they can be embedded within a larger
39+ expression. In the next example, we’ll see how a lambda function is created
40+ within a function call. In this case, there is no global variable to store the
41+ values of the lambda function:
2542
2643.. code-block :: pycon
2744 :linenos:
0 commit comments