I was asked to write a function in Python 3 for: Write a function called
general_poly, that would, for example, evaluate
general_poly([1, 2, 3, 4])(10) to
1*10^3 + 2*10^2 + 3*10^1 + 4*10^0.
Now I don't understand what the second parenthesis,
How would my function
general_poly know, to take that value inside itself and use it?
It means that your function should return a function:
def multiplier(times): def inner(num): return num * times return inner result = multiplier(2)(3) print(result) # 6 times_four = multiplier(4) print(times_four(3)) # 12
This may or may not be an overkill, but if you need to save state you can even use a class and implement
class multiplier: def __init__(self, times): self.times = times def __call__(self, num): return num * self.times print(multiplier(2)(3)) # 6