Dr. Dobb's Python-URL! - weekly Python news and links (Dec 7)

M

Marc 'BlackJack' Rintsch

Chris said:
functions with real names is crucial to maintainable code. The only
reason to ever use a lamdba in Python is because you don't want to
give a function a name, and that is just not a compelling use case for
GUI events.

Ah, but that neglects the sheer utility of delayed-evaluation
expressions. Consider the key= parameter to list.sort, et. al:

complicated_list.sort(key=lambda x: x[3])

This can be written as::

from operator import itemgetter
complicated_list.sort(key=itemgetter(3))

Ciao,
Marc 'BlackJack' Rintsch
 
B

bonono

Marc said:
Chris said:
functions with real names is crucial to maintainable code. The only
reason to ever use a lamdba in Python is because you don't want to
give a function a name, and that is just not a compelling use case for
GUI events.

Ah, but that neglects the sheer utility of delayed-evaluation
expressions. Consider the key= parameter to list.sort, et. al:

complicated_list.sort(key=lambda x: x[3])

This can be written as::

from operator import itemgetter
complicated_list.sort(key=itemgetter(3))
Though I find the lambda form easier for me to understand but
itemgetter should be the preferred way as it should have some
performance advantage and sort are those kind of functions that need
it, in general.
 
C

Cameron Laird

.
.
.
# create numeric pad
digit("7", 1, 1); digit("8", 2, 1); digit("9", 3, 1)
digit("4", 1, 2); digit("5", 2, 2); digit("6", 3, 2)
digit("1", 1, 3); digit("2", 2, 3); digit("3", 3, 3)
.
.
.
I confess that this sort of thing often tempts me to

for (numeral, row, column) in [
(7, 1, 1), (8, 2, 1), (9, 3, 1),
(4, 1, 2), (5, 2, 2), (6, 3, 2),
(1, 1, 3), (2, 2, 3), (3, 3, 3)]:
digit(str(numeral), row, column)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,221
Latest member
TrinidadKa

Latest Threads

Top