In one of the training parts of my course I was given the following question:
Write a function make_multiplier(factor) that returns a function which takes an argument x and which should return factor * x.
For example:
f=make_multiplier(10)
f(1)
10
f(2)
20
I have absolutely no idea where to start with this one; I have scrummaged through all my notes and cant find anything useful.
Could someone please give me a hint or point me in the right direction of what I need to do?
解决方案
Here is a function that returns a function:
def foo():
def bar():
return 42
return bar
You can call it like so:
foo()() # 42
# or
baz = foo()
baz() # 42
There's your hint.