<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 1, 2013 at 12:58 PM, Alexander Shorin <span dir="ltr"><<a href="mailto:kxepal@gmail.com" target="_blank">kxepal@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Steven,<br>
<div class="im"><br>
On Thu, Aug 1, 2013 at 7:06 PM, Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
> Hi Alexander,<br>
><br>
> On 02/08/13 00:48, Alexander Shorin wrote:<br>
>><br>
>> Hi Ronald,<br>
>><br>
>> I understand this, but I'm a bit confused about fate of lambdas with<br>
>> such guideline since I see no more reasons to use them with p.9<br>
>> statement: long lines, code duplicate, no mock and well tests etc. -<br>
>> all these problems could be solved with assigning lambda to some name,<br>
>> but now they are looks useless (or useful only for very trivial cases)<br>
><br>
><br>
> Lambda is still useful for the reason lambda has always been useful: it is<br>
> an expression, not a statement, so you can embed it directly where needed.<br>
><br>
> # Preferred:<br>
> sorted(data, key=lambda value: value['spam'].casefold())<br>
><br>
> # Allowed:<br>
> def f(value): return value['spam'].casefold()<br>
> sorted(data, key=f)<br>
><br>
> # Prohibited:<br>
> f = lambda value: value['spam'].casefold()<br>
> sorted(data, key=f)<br>
><br>
> # SyntaxError:<br>
> sorted(data, key=def f(value): value['spam'].casefold())<br>
<br>
</div>The case:<br>
<br>
items =  [[0, 'foo'], [3, 'baz'],  [2, 'foo'], [1, 'bar']]<br>
<br>
Need to group by second item. Quite common task:<br>
<br>
>>> from itertools import groupby<br>
>>><br>
>>> for key, items in groupby(items, key=lambda i: i[1]):<br>
>>>   print(key, ':', list(items))<br>
foo : [[0, 'foo']]<br>
baz : [[3, 'baz']]<br>
foo : [[2, 'foo']]<br>
bar : [[1, 'bar']]<br>
<br>
oops, failed, we need to sort things first by this item and it looks<br>
we have to duplicate grouping function:<br>
<br>
fun = lambda i: i[1]<br>
for key, items in groupby(sorted(items, key=fun), key=fun):<br>
  print(key, ':', list(items))<br>
<br>
Ok, PEP suggests to use defs, so we adds 3 more lines (before and<br>
after def + return) to code:<br>
<br>
def fun(i):<br>
  return i[1]<br>
<br>
for key, items in groupby(sorted(items, key=fun), key=fun):<br>
  print(key, ':', list(items))<br>
<br>
so that's the question: what is the rationale of this if lambdas<br>
successfully solves the problem with minimal amount of typing, code<br>
and thinking? I thought there should be only one way to do something,<br>
but this PEP-8 statement conflicts with PEP-20 one:<br>
<br>
> There should be one-- and preferably only one --obvious way to do it.<br>
<br>
It's really not oblivious why lambdas couldn't be assignment to some<br>
name, especially in the light of fact that if they are been passed to<br>
some function as argument, they will be assignee to some name.<br></blockquote><div><br></div><div> Just because you can doesn't mean you should.</div><div><br></div><div>This guideline is all about being explicit over implicit, not about saving typing. If you want to bind a function to a name then you should use a def to specify that fact; you also lose some things otherwise (e.g. __name__ is not set). Lambdas should be thought of one-off functions you write inline because it expresses the intent of the code just as well. Assigning a lambda to a variable is in no way more beneficial compared to using def and thus this guideline suggesting you use def to make it at least as clear, if not more and to gain benefits such as __name__ being set (which helps with debugging, etc.).</div>

</div></div></div>