I can't tell what your code looks like because it is not indented.
doubler is now f(2) = return g so that is removed and the value from doubler(5) gets assigned to y that is why you get the x*y value and not "function g at blah blah" which is what the return g would print. Add some print statements
def f(x):
print " f entered", x
def g(y):
print "g executed", y
return x * y
return g
doubler = f(2)
print doubler(5)
## if you want to look at it like this: doubler as f(2) =
x = 2
def g(y):
return x * y
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9
Another look at closure ...
''' closure102.py
A nested function has access to the environment in which it was defined.
This definition occurs during the execution of the outer function.
In a closure you return the reference to the inner function that remembers
the state of the outer function, even after the outer function has completed
execution.
'''
def outer(x):
def inner(y):
return x * y
# return reference to the inner function
return inner
# inner remembers x = 2
inner = outer(2)
print(inner)
print('-'*30)
# this call will supply the y = 7
print(inner(7))
''' result (location in memory) ...
<function inner at 0x02C4D738>
------------------------------
14
'''
vegaseat
DaniWeb's Hypocrite
6,478 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 37
Question Answered as of 4 Months Ago by
vegaseat,
woooee,
snippsat
and 1 other