I appreciate this is a difficult one to describe, but things like Tkinter, PyGame or anything after a callback (calling another function when something happens) can throw up complications when you want to pass an argument.
Consider the following line:
threading.Timer(5, bar).start()
That’s fairly straight forward that it’s calling the function bar after 5 seconds. However, what happened if you wanted to call bar with an argument? Just replacing bar with bar(test) wouldn’t work as it would call that function when the line is first encountered, not after 5 seconds.
There are two ways to solve this. Partial and Lambda. The two lines below are very similar in function:
threading.Timer(5, partial(foo, myNewVar)).start()
threading.Timer(5, lambda: foo(myNewVar)).start()
The main difference between the two is when the value of myNewVar is retrieved from memory. In the first example, partial, it’s retrieved when you call partial. So the value of myNewVar will be the value at the point the timer is started.
The second example, lambda, the value of myNewVar is retrieved when it is called, so if myNewVar changes in the 5 seconds before it’s run, it will take the new value whereas partial would still take the original value.
This can be explored in the attached Python file.