python: How can I remove lambda in this code?

Viewed 133

Im new at programming and have no clue how to use lambda in programming. Im asking if there is a way to edit this code so that I dont need to use lambda on it.

from tkinter import *
def define(a):
    pass

root = Tk()

#this line
auto = Button(root, text="auto", command=lambda: define(True)).pack()
2 Answers

If you want to remove lambda from your code you must define a function return another function define.

That is,

def replacement():
    return define(argument)

This way you don't have to use lambda in calling Button. Instead you would call Button as :

auto = Button(root, text="auto", command=replacement).pack()

I hope this helps. But, you should learn what lambda's are.

You can also use partial from functools. It is basically the same as what @Suraj Upadhyay suggested. This is the code:

from tkinter import *
from functools import partial

def define(a):
    pass

root = Tk()

function = partial(define, True) # The first arg is the function name and the rest are the function args
auto = Button(root, text="auto", command=function)
auto.pack()
Related