I want to define a function, e.g. a polynomial, from a list of prefactors. Something like
order = [1,0,1]
def poly(x):
res = 0
for i, o in enumerate(order):
res += o * x**i
return res
so poly(x)
returns 1 + x²
.
I need to call that function multiple times for different x
, but with the same prefactors. The above function executes the for loop every time it is called, which is rather inefficient, especially if the order
list is long.
How can I loop only once and call the result for different x
? What is the pythonic solution?