I have the following code, which fails:
def test_function_combining_wrong():
def one():
print("ONE")
return "ONE"
def two():
print("TWO")
return "TWO"
F1S = {
1: one,
2: two,
}
F2S = {
**{key: lambda: value() for key, value in F1S.items()},
}
print("1st Call")
assert F1S[1]() == "ONE"
assert F1S[2]() == "TWO"
print("2nd Call")
assert F2S[1]() == "ONE" # ATTENTION: THIS IS WRONG WEIRD PYTHON STUFF
assert F2S[2]() == "TWO"
In my head this code should work and I would like to know why Python returns TWO
for the F2S[1]()
. I'm trying to nest some lambda functions together in dictionaries.