+ 4
When and why should I use lambda functions?
I'm trying to figure out how and when Python lambdas should be used, and why should I use them instead of regular functions.
6 ответов
+ 11
when you are in hurry or near deadline.
+ 10
When there is no point in defining a regular, named function, i.e. if you only use it once - as a key in sorted() or in map(), filter(), reduce() situations.
+ 5
- Functional programming
- Little callback function
- ...
https://en.wikipedia.org/wiki/Functional_programming#JUMP_LINK__&&__Python__&&__JUMP_LINK
+ 2
# I find they are handy to
# keep simple operations close
# to associated code sometimes
import tkinter as tk
root = tk.Tk()
tk.Button(text='Double click Me').grid()
b=root.winfo_children()[0]
b.bind("<Double-1>",lambda event:b.configure(text='clicked twice'))
tk.Button(text='text here',
command=lambda:anon_click_command('top')).grid()
tk.Button(text='text here',
command=lambda:anon_click_command('bottom')).grid()
def anon_click_command(button_id):
c= root.winfo_children()[1:]
for but in c:
if but['text']!='text here':
but['text']='text here'
if button_id =='top':
c[0].configure(text='changed top')
elif button_id =='bottom':
c[1].configure(text='changed bottom')
root.mainloop()
+ 1
@Agus damn true 😂😂😂
0
List Comprehensions 5.1.3 says that Lambda functions can be used to calculate List Comprehensions without any "side effects". (https://docs.python.org/3/tutorial/datastructures.html)