[Tutor] Are you allowed to shoot camels? [kinda OT]
Chad Crabtree
flaxeater at yahoo.com
Fri Feb 4 03:54:18 CET 2005
How about a concrete example where lambda is more elegant than a
named
block of code
aList=['a','bb','ccc','dddd','ee']
bList=aList[:] #deep copy
assert not bList is aList
def sortByLength(item1,item2):
return cmp(len(item1),len(item2))
bList.sort(sortByLength)
assert bList==['a', 'bb', 'ee', 'ccc', 'dddd']
aList.sort(lambda x,y:cmp(len(x),len(y)))
assert aList==['a', 'bb', 'ee', 'ccc', 'dddd']
Now this is a concrete example of how lambda simplifies code, at
least
for me because it does not clutter my mental name space. Also it is
much shorter. However it should be said that this is very much a
question of taste. However I must say that lambda's are very useful
even necessary for using Tkinter.
Here's something else, while not exactly the same but an
illustration.
aFuncList=[]
def x():
print "one"
aFuncList.append(x)
def x():
print "two"
aFuncList.append(x)
def x():
print "three"
aFuncList.append(x)
for item in aFuncList:
item()
In summary there has been a great deal of argument about lambda's,
even
on this mostly sanguine mailing list. I feel that it's one of those
strange things about python true. Guido purposely made it very
limited
because it could get hairy very fast with more powerful lambda's, the
functional people would make code that would break newcomers heads.
God
knows I had a hard enough time with them at first.
__________________________________
Do you Yahoo!?
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250
More information about the Tutor
mailing list