...while still leaving it executable within the function.
The idea behind this is I want to create a summation function. Here's what I have so far:
def summation(n, bound, operation):
if operation is None and upper != 'inf':
g = 0
for num in range(n, limit + 1):
g += num
return g
else:
pass
But summations are most often about infinite convergent series (for which I use 'inf'
), with operations applied to each term. Ideally, I'd like to be able to write print summation(0, 'inf', 1 / factorial(n))
and get the mathematical constant e, or def W(x): return summation(1, 'inf', ((-n) ** (n - 1)) / factorial(n))
to get the Lambert W function.
All that comes to my mind is passing the appropriate arithmetic as a string and then using the exec
statement to execute it. But I don't think that would accomplish the whole thing, and it's obviously dangerous to use exec
with possibly user-entered code.