It's not hard to understand "functions are objects we can return a function from another function". But how is below code working?
# Functions can return another function
def create_adder(x):
def adder(y):
return x+y
return adder
add_15 = create_adder(15)
print (add_15(10))
The result is 25.
Personal understanding:
create_adder(x) function will return the reference of adder function, kind like:
<function create_adder.<locals>.adder at 0x7fd83e19fe18>
15 is x in it(create_adder) and add_15 is object of create_adder function, so add_15(10) might have taken x as the argument.Then how did it get the value of y?No variable created for it and no argument passed for it?
Can someone help me point out the misunderstanding?
defof adder, it saysy.