[Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Steven D'Aprano

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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Alexander Shorin
Hi Steven,

On Thu, Aug 1, 2013 at 7:06 PM, Steven D'Aprano st...@pearwood.info 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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Chris Angelico
On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin kxe...@gmail.com 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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Brett Cannon
On Thu, Aug 1, 2013 at 12:58 PM, Alexander Shorin kxe...@gmail.com wrote:

 Hi Steven,

 On Thu, Aug 1, 2013 at 7:06 PM, Steven D'Aprano st...@pearwood.info
 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.).
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Stephen J. Turnbull
Chris Angelico writes:
  On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin kxe...@gmail.com 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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread André Malo
* Stephen J. Turnbull wrote:

 Chris Angelico writes:
   On Thu, Aug 1, 2013 at 5:58 PM, Alexander Shorin kxe...@gmail.com 
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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Lambda [was Re: PEP 8 modernisation]

2013-08-01 Thread Vito De Tullio
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@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com