473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can you pass functions as arguments?

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?

Dec 19 '05 #1
4 1404
bo*******@yahoo.com writes:
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?


You can just pass f as an argument. The following is not the most
concise or general way, it just shows how you can pass a function.

def square(x):
return x**2

def sum100(f): # f(0)+f(1)+...+f(100)
s = 0
for i in xrange(101):
s += f(i)
return s

print sum100(square) # pass square as an argument
Dec 19 '05 #2
bo*******@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*.
Dec 19 '05 #3
On 18 Dec 2005 23:18:48 -0800, Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
bo*******@yahoo.com writes:
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?


You can just pass f as an argument. The following is not the most
concise or general way, it just shows how you can pass a function.

def square(x):
return x**2

def sum100(f): # f(0)+f(1)+...+f(100)
s = 0
for i in xrange(101):
s += f(i)
return s

print sum100(square) # pass square as an argument


or
def square(x): return x*x ... from itertools import imap
sum(imap(square, xrange(101))) 338350

which seems to agree with def sum100(f): ... s = 0
... for i in xrange(101):
... s += f(i)
... return s
... sum100(square) 338350

or if the OP actually wants the specific function, def sum100a(f): return sum(imap(f, xrange(101))) ... sum100a(square)

338350

Regards,
Bengt Richter
Dec 19 '05 #4
bo**@oz.net (Bengt Richter) writes:
or if the OP actually wants the specific function,
>>> def sum100a(f): return sum(imap(f, xrange(101))) ... >>> sum100a(square)

338350


Similarly with generator comprehension, if I have the syntax right:

def sum100c(f): return sum(f(i) for i in xrange(101))
Dec 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
12
by: BartlebyScrivener | last post by:
I'm still new at this. I can't get this to work as a script. If I just manually insert the values for sys.argv and sys.argv it works fine, but I can't pass the variables from the command line. What...
5
by: FrederikVds | last post by:
I have a function which can be called with an unlimited number of arguments. I want to call another function with exactly the same arguments. I know I can get the arguments in the arguments...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.