The lambda
expression is used to define a one-line (anonymous) function. Thus, writing
f = lambda x: 2*x
is (more or less) equivalent to
def f(x):
return 2*x
Further, as for normal functions, it is possible to have default arguments also in a lambda
function. Thus
k = 2
f = lambda x=k: 2*x
can be called either as f()
where x
will be the value of k
i.e. 2
or as f(3)
where x = 3
. Now, to make it even more strange, since the lambda
function has its own name space, it is possible to exchange the k
with x
as
x = 2
f = lambda x=x: 2*x
which then means that the default value of f()
will be the (outer) x
value (i.e. 2
) if no other parameter is set.
Thus, the expression
lambda app=app: openApp(Apps[app])
is a function (unnamed) that takes one argument app
with the default value of the (outer) variable app
. And when called, it will execute the instruction openApp(Apps[app])
where Apps
is a global (outer) object.
Now, why would we want that, instead of just writing command=openApp(Apps[app])
when calling .add_command()
? The reason is that we want the command to execute not when defining the command, i.e., when calling .add_command()
, but at a later time, namely when the menu item is selected.
Thus, to defer to the execution we wrap the execution of openApp(Apps[app])
in a function. This function will then be executed when the menu item is selected. Thereby we will call openApp
then, instead of right now (at the time we want to add the command) which would be the case otherwise.
Now, since the lambda
function is called with no arguments, it will use its default parameter (outer) app
as (inner) app
. That makes the whole argument a bit redundant, as the outer variable is directly accessible within the lambda
expression. Therefore, it is possible to simplify the expression to lambda: openApp(Apps[app])
i.e. wiht no arguments, making the full line
appsMenu.add_command(label=app, command=lambda: openApp(Apps[app]))
To me, this looks a bit simpler, and is the way I would write it, but conceptually there is no difference between this and the code with default parameters.