The author of the top answer of this question uses a lamda function as parameter for this function:
>>> d1 = {'one':1, 'both':3, 'falsey_one':False, 'falsey_both':None}
>>> d2 = {'two':2, 'both':30, 'falsey_two':None, 'falsey_both':False}
def dict_ops(d1, d2, setop):
... """Apply set operation `setop` to dictionaries d1 and d2
...
... Note: In cases where values are present in both d1 and d2, the value from
... d1 will be used.
... """
... return {k:d1.get(k,k in d1 or d2[k]) for k in setop(set(d1), set(d2))}
Like this:
>>> print "d1 - d2:", dict_ops(d1, d2, lambda x,y: x-y)
Which returns:
d1 - d2: {'falsey_one': False, 'one': 1}
I tried to do the same thing but not as a function, because I want to understand how the setop part works.
{k:d1.get(k,k in d1 or d2[k]) for k in lambda d1,d2: set(d1) - set(d2)}
However this code returns a syntax error.
But this works:
l = lambda d1,d2: set(d1) - set(d2)
{k:d1.get(k,k in d1 or d2[k]) for k in l(d1,d2)}
Why does the second solution work, but the first one dont?
If I call the dict_ops function with these parameters (d1, d2, lambda x,y: x-y) how does setop(set(d1) - set(d2) look like?
lambda.(lambda d1,d2: set(d1) - set(d2))(d1, d2)would work.lambdafunction with paramters? Was my mistake that I just 'wrote' alambdafunction without providing parameters?