I have a back-ground of C++ and trying to learn some python.
Whilst i understand virtual functions for C++, I unfortunately do not understand what is meant by late binding of closures in python.
Link: https://gist.github.com/deemson/8efabf56d67623ead804 (no longer works)
Copy-pasted from a tutorial:
functions = []
for n in [1, 2, 3]:
def func(x):
return n*x
functions.append(func)
# You would expect this to print [2, 4, 6]
print(
'calling a list of bad closures and output is: {}'
.format(str([function(2) for function in functions]))
)
What exactly is happening here? When the function is appended in to the list, what values does it have? Can someone please simplify this code for me to understand?