Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Lambda [was Re: PEP 8 modernisation]

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


steve at pearwood

Aug 1, 2013, 8:06 AM

Post #1 of 7 (63 views)
Permalink
Lambda [was Re: PEP 8 modernisation]

Hi Alexander,

On 02/08/13 00:48, Alexander Shorin wrote:
> Hi Ronald,
>
> I understand this, but I'm a bit confused about fate of lambdas with
> such guideline since I see no more reasons to use them with p.9
> statement: long lines, code duplicate, no mock and well tests etc. -
> all these problems could be solved with assigning lambda to some name,
> but now they are looks useless (or useful only for very trivial cases)

Lambda is still useful for the reason lambda has always been useful: it is an expression, not a statement, so you can embed it directly where needed.

# Preferred:
sorted(data, key=lambda value: value['spam'].casefold())

# Allowed:
def f(value): return value['spam'].casefold()
sorted(data, key=f)

# Prohibited:
f = lambda value: value['spam'].casefold()
sorted(data, key=f)

# SyntaxError:
sorted(data, key=def f(value): value['spam'].casefold())



--
Steven
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


kxepal at gmail

Aug 1, 2013, 9:58 AM

Post #2 of 7 (61 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

Hi Steven,

On Thu, Aug 1, 2013 at 7:06 PM, Steven D'Aprano <steve [at] pearwood> wrote:
> Hi Alexander,
>
> On 02/08/13 00:48, Alexander Shorin wrote:
>>
>> Hi Ronald,
>>
>> I understand this, but I'm a bit confused about fate of lambdas with
>> such guideline since I see no more reasons to use them with p.9
>> statement: long lines, code duplicate, no mock and well tests etc. -
>> all these problems could be solved with assigning lambda to some name,
>> but now they are looks useless (or useful only for very trivial cases)
>
>
> Lambda is still useful for the reason lambda has always been useful: it is
> an expression, not a statement, so you can embed it directly where needed.
>
> # Preferred:
> sorted(data, key=lambda value: value['spam'].casefold())
>
> # Allowed:
> def f(value): return value['spam'].casefold()
> sorted(data, key=f)
>
> # Prohibited:
> f = lambda value: value['spam'].casefold()
> sorted(data, key=f)
>
> # SyntaxError:
> sorted(data, key=def f(value): value['spam'].casefold())

The case:

items = [[0, 'foo'], [3, 'baz'], [2, 'foo'], [1, 'bar']]

Need to group by second item. Quite common task:

>>> from itertools import groupby
>>>
>>> for key, items in groupby(items, key=lambda i: i[1]):
>>> print(key, ':', list(items))
foo : [[0, 'foo']]
baz : [[3, 'baz']]
foo : [[2, 'foo']]
bar : [[1, 'bar']]

oops, failed, we need to sort things first by this item and it looks
we have to duplicate grouping function:

fun = lambda i: i[1]
for key, items in groupby(sorted(items, key=fun), key=fun):
print(key, ':', list(items))

Ok, PEP suggests to use defs, so we adds 3 more lines (before and
after def + return) to code:

def fun(i):
return i[1]

for key, items in groupby(sorted(items, key=fun), key=fun):
print(key, ':', list(items))

so that's the question: what is the rationale of this if lambdas
successfully solves the problem with minimal amount of typing, code
and thinking? I thought there should be only one way to do something,
but this PEP-8 statement conflicts with PEP-20 one:

> There should be one-- and preferably only one --obvious way to do it.

It's really not oblivious why lambdas couldn't be assignment to some
name, especially in the light of fact that if they are been passed to
some function as argument, they will be assignee to some name.



--
,,,^..^,,,
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


rosuav at gmail

Aug 1, 2013, 10:01 AM

Post #3 of 7 (61 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin <kxepal [at] gmail> wrote:
> fun = lambda i: i[1]
> for key, items in groupby(sorted(items, key=fun), key=fun):
> print(key, ':', list(items))

I'd do a direct translation to def here:

def fun(i): return i[1]
for key, items in groupby(sorted(items, key=fun), key=fun):
print(key, ':', list(items))

ChrisA
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


vito.detullio at gmail

Aug 1, 2013, 10:55 AM

Post #4 of 7 (58 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

Steven D'Aprano wrote:

> Lambda is still useful for the reason lambda has always been useful: it is
> an expression, not a statement, so you can embed it directly where needed.

are there some possibilities to change def to an expression? do I need to
wait 'till python9k?

yes, this brings to the possibility to write something like

foo = def bar():
pass

but at least should let the lambda to die in peace...


--
ZeD

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


brett at python

Aug 1, 2013, 12:33 PM

Post #5 of 7 (59 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

On Thu, Aug 1, 2013 at 12:58 PM, Alexander Shorin <kxepal [at] gmail> wrote:

> Hi Steven,
>
> On Thu, Aug 1, 2013 at 7:06 PM, Steven D'Aprano <steve [at] pearwood>
> wrote:
> > Hi Alexander,
> >
> > On 02/08/13 00:48, Alexander Shorin wrote:
> >>
> >> Hi Ronald,
> >>
> >> I understand this, but I'm a bit confused about fate of lambdas with
> >> such guideline since I see no more reasons to use them with p.9
> >> statement: long lines, code duplicate, no mock and well tests etc. -
> >> all these problems could be solved with assigning lambda to some name,
> >> but now they are looks useless (or useful only for very trivial cases)
> >
> >
> > Lambda is still useful for the reason lambda has always been useful: it
> is
> > an expression, not a statement, so you can embed it directly where
> needed.
> >
> > # Preferred:
> > sorted(data, key=lambda value: value['spam'].casefold())
> >
> > # Allowed:
> > def f(value): return value['spam'].casefold()
> > sorted(data, key=f)
> >
> > # Prohibited:
> > f = lambda value: value['spam'].casefold()
> > sorted(data, key=f)
> >
> > # SyntaxError:
> > sorted(data, key=def f(value): value['spam'].casefold())
>
> The case:
>
> items = [[0, 'foo'], [3, 'baz'], [2, 'foo'], [1, 'bar']]
>
> Need to group by second item. Quite common task:
>
> >>> from itertools import groupby
> >>>
> >>> for key, items in groupby(items, key=lambda i: i[1]):
> >>> print(key, ':', list(items))
> foo : [[0, 'foo']]
> baz : [[3, 'baz']]
> foo : [[2, 'foo']]
> bar : [[1, 'bar']]
>
> oops, failed, we need to sort things first by this item and it looks
> we have to duplicate grouping function:
>
> fun = lambda i: i[1]
> for key, items in groupby(sorted(items, key=fun), key=fun):
> print(key, ':', list(items))
>
> Ok, PEP suggests to use defs, so we adds 3 more lines (before and
> after def + return) to code:
>
> def fun(i):
> return i[1]
>
> for key, items in groupby(sorted(items, key=fun), key=fun):
> print(key, ':', list(items))
>
> so that's the question: what is the rationale of this if lambdas
> successfully solves the problem with minimal amount of typing, code
> and thinking? I thought there should be only one way to do something,
> but this PEP-8 statement conflicts with PEP-20 one:
>
> > There should be one-- and preferably only one --obvious way to do it.
>
> It's really not oblivious why lambdas couldn't be assignment to some
> name, especially in the light of fact that if they are been passed to
> some function as argument, they will be assignee to some name.
>

Just because you can doesn't mean you should.

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.).


stephen at xemacs

Aug 1, 2013, 12:41 PM

Post #6 of 7 (59 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

Chris Angelico writes:
> On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin <kxepal [at] gmail> wrote:
> > fun = lambda i: i[1]
> > for key, items in groupby(sorted(items, key=fun), key=fun):
> > print(key, ':', list(items))
>
> I'd do a direct translation to def here:
>
> def fun(i): return i[1]
> for key, items in groupby(sorted(items, key=fun), key=fun):
> print(key, ':', list(items))

As long as it's about readability, why not make it readable?

def second(pair): return pair[1]
for key, items in groupby(sorted(items, key=second), key=second):
print(key, ':', list(items))

I realize it's somewhat unfair (for several reasons) to compare that
to Alexander's "fun = lambda i: i[1]", but I can't help feeling that
in another sense it is fair.

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


nd at perlig

Aug 1, 2013, 12:49 PM

Post #7 of 7 (59 views)
Permalink
Re: Lambda [was Re: PEP 8 modernisation] [In reply to]

* Stephen J. Turnbull wrote:

> Chris Angelico writes:
> > On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin <kxepal [at] gmail>
wrote:
> > > fun = lambda i: i[1]
> > > for key, items in groupby(sorted(items, key=fun), key=fun):
> > > print(key, ':', list(items))
> >
> > I'd do a direct translation to def here:
> >
> > def fun(i): return i[1]
> > for key, items in groupby(sorted(items, key=fun), key=fun):
> > print(key, ':', list(items))
>
> As long as it's about readability, why not make it readable?
>
> def second(pair): return pair[1]
> for key, items in groupby(sorted(items, key=second), key=second):
> print(key, ':', list(items))
>
> I realize it's somewhat unfair (for several reasons) to compare that
> to Alexander's "fun = lambda i: i[1]", but I can't help feeling that
> in another sense it is fair.

Seems to run OT somewhat, but "second" is probably a bad name here. If the
key changes, you have to rename it in several places (or worse, you DON'T
rename it, and then the readability is gone).
Usually I'm using a name with "key" in it - describing what it's for, not
how it's done. The minimal distance to its usage is supporting that, too.

nd
--
"Das Verhalten von Gates hatte mir bewiesen, dass ich auf ihn und seine
beiden Gefährten nicht zu zählen brauchte" -- Karl May, "Winnetou III"

Im Westen was neues: <http://pub.perlig.de/books.html#apache2>
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.