so I recently wrote a function that sorted using the last value in a tuple. Obviously this couldn't be done simply say sorted(tuples, key=tuples[-1]) because that is not a "legal" call. However I did run across, while trying to figure out how to make things like this work, the utilization of lambda so the code became.
The code runs correctly and, that's all well and good, but I don't understand why lambda tuples"or any given var for that matter":. makes this possible. Can someone explain this to me in a way I'll understand so that I can utilize the principle to its maximum potential?
p.s. I have essentially no calculus experience.
def dummy(x, y, z):
return expression(x, y, z)
key = dummy
It means that lambda is only a way to write small, anonymous functions. The main restriction is that the body of a lambda form is not a sequence of statements but a single python expression (note that the keyword 'return' does not appear in the lambda form).
Lambda forms come from a branch of mathematical logic called "lambda calculus" which studies the logical consequences of an abstract functional language. These lambda forms are the basis of most so-called functional programming languages and that's where python borrowed the construct (together with list comprehensions and generators).
I tend to believe that lambda forms are not very useful in python. Writing a genuine small function is not less efficient and it is more flexible (it leaves the possibility to add statements in the body, if only a print or assert statement for debugging).
It is also _very_ heavily used in GUI development, as events (scrolling, etc). Of course, if you can use a list comp instead of these, then do so. They're usually more readable.
IMO, if you assign lambda to something, it shouldn't be used, instead you should define a method using def. In production code, use it where it's useful, but try to use it as little as possible (don't assign it to a name)
Here the example with functools.partial examples for general function and button call backs added. Clear names are absolute must, but sometimes descriptive docstring is better than too long name, which is still unclear. Notice that in ide like IDLE, you get tooltip of first line of docstring, when you type functions name.
from functools import partial
from Tkinter import *
def exceptions_key(word, exceptional):
""" exceptional first, then others, first empty string, however.
word = word.lower()
order = exceptional.lower()+ ''.join(c for c in string.lowercase)
return (not word and -1) or order.find(word), word
xword = xword.lower()
return (not xword.startswith('x'), xword)
def number_to_title(root, number):
root.title('%s pushed' % number)
notinorder = ['mix', 'xyz', '', 'Apple', 'Xanadu', 'aardvark', 'min', 'sun']
print(sorted(notinorder, key=lambda x: (not x.startswith(('x','X')), x.lower())))
print(sorted(notinorder, key=lambda x: (x and x not in 'xX', x.lower())))
# both x and m exceptions
print(sorted(notinorder, key=partial(exceptions_key, exceptional='xm')))
root = Tk()
for i in range(1,20):
root=root, number=i)).pack(side=LEFT, padx=4, pady=4)