""" Ex. 7.12 from "A primer on..." Implement a class for a sum. The function defining the terms of the sum should be an argument to the constructor. The sum should have a __call__ method which evaluated the sum for a given x. """ class Sum: def __init__(self, term, M, N): self.term = term self.M = M self.N = N def __call__(self, x): s = 0 for k in range(self.M, self.N+1): s += self.term(k,x) return s def term(k, x): return (-x)**k S = Sum(term, M=0, N=3) x = 0.5 print(S(x)) print(S.term(k=4, x=x)) # (-0.5)**4 """ Terminal> python Sum.py 0.625 0.0625 """