I am reading the book for web2py. It is running through some python syntax examples. When I ge to this closure example I am not understanding how 'y' gets its value.
This is the code.
>>> def f(x): def g(y): return x * y return g >>> doubler = f(2) # doubler is a new function >>> tripler = f(3) # tripler is a new function >>> quadrupler = f(4) # quadrupler is a new function >>> print doubler(5) 10 >>> print tripler(5) 15 >>> print quadrupler(5) 20
so when they create doubler
doubler = f(2)
y gets the value of 2 when this new function is created. However f(2) is from f(x). This is where my confusion is, how does y get its value from x?