Nov-16-2021, 10:20 PM
(This post was last modified: Nov-16-2021, 10:20 PM by deanhystad.)
Whatever works is what's best.
The lambda expression works because it is essentially the same as doing this:
When you want the arguments to change from call to call this is a good thing. Usually this is not the desired behavior and you need to modify the lambda expression to call your function with static values. Something like this:
The lambda expression works because it is essentially the same as doing this:
def lambdafunc(): calcGrowth(numbers[0], numbers[1]) btn5 = tk.Button(frame1, text='calculate exponent', command=lambdafunc)When you press the button you don't call calcGrowth, you call the function created by the lambda expression. This function calls calcGrowth, but as always the arguments are evaluated before the function gets called.
When you want the arguments to change from call to call this is a good thing. Usually this is not the desired behavior and you need to modify the lambda expression to call your function with static values. Something like this:
btn5 = tk.Button(frame1, text='calculate exponent', command=lambda a=numbers[0], b=numbers[1]: calcGrowth(a, b)In this example the lambda expression creates a function with arguments. For arguments sake lets say numbers[0] is 42 and numbers[1] is 3.14.
def lambdafunc(a=42, b=3.14): calcGrowth(a, b) btn5 = tk.Button(frame1, text='calculate exponent', command=lambdafunc)partial provides a way to do the same thing with a cleaner syntax and without the extra function