Can you pass functions as arguments?

Xavier Morel xavier.morel at masklinn.net
Mon Dec 19 07:49:59 EST 2005


bobueland at yahoo.com wrote:
> I want to calculate f(0) + f(1) + ...+ f(100) over some function f
> which I can change. So I would like to create a function taking f as
> argument giving back the sum. How do you do that in Python?
> 
Python functions (and classes, and modules) are first-class objects, so 
you can use them as regular objects: send them as arguments, generate 
and return them from functions, ...

BTW, if your goal is to create that kind of sums of function results, I 
think you'd be interested in using both Python's _generators_ and the 
built-in function *sum*.



More information about the Python-list mailing list