473,399 Members | 4,254 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,399 software developers and data experts.

arbitrary number of arguments in a function declaration

rbt
How do I set up a function so that it can take an arbitrary number of
arguments? For example, I have a bunch of expenses which may grow or
shrink depending on the client's circumstance and a function that sums
them up... hard coding them is tedious. How might I make this dynamic so
that it can handle any amount of expenses?

def tot_expenses(self, e0, e1, e2, e3):
pass
Jul 18 '05 #1
4 2052
rbt wrote:
How do I set up a function so that it can take an arbitrary number of
arguments? For example, I have a bunch of expenses which may grow or
shrink depending on the client's circumstance and a function that sums
them up... hard coding them is tedious. How might I make this dynamic so
that it can handle any amount of expenses?

def tot_expenses(self, e0, e1, e2, e3):
pass


The Python Tutorial is a wonderful thing. . .

Anyway, you can either set up your function to take a proper list, and then
discover that the sum function already exists to add up the contents of a list:

def tot_expenses(self, expenses):
self.total_expenses = sum(expenses)

Or, have the function take a variable number of arguments, and do the same thing:

def tot_expenses(self, *args):
self.total_expenses = sum(args)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #2
rbt
Nick Coghlan wrote:
rbt wrote:
How do I set up a function so that it can take an arbitrary number of
arguments? For example, I have a bunch of expenses which may grow or
shrink depending on the client's circumstance and a function that sums
them up... hard coding them is tedious. How might I make this dynamic
so that it can handle any amount of expenses?

def tot_expenses(self, e0, e1, e2, e3):
pass

The Python Tutorial is a wonderful thing. . .


But so is this list ;)
Anyway, you can either set up your function to take a proper list, and
then discover that the sum function already exists to add up the
contents of a list:

def tot_expenses(self, expenses):
self.total_expenses = sum(expenses)

Or, have the function take a variable number of arguments, and do the
same thing:

def tot_expenses(self, *args):
self.total_expenses = sum(args)

Cheers,
Nick.


Many thanks!
Jul 18 '05 #3
rbt wrote:
How do I set up a function so that it can take an arbitrary number of
arguments?
If you haven't already, you should check out the Tutorial:

http://docs.python.org/tut/node6.htm...00000000000000
How might I make this dynamic so
that it can handle any amount of expenses?

def tot_expenses(self, e0, e1, e2, e3):
pass


py> class C(object):
.... def tot_expenses(self, *expenses):
.... print expenses
....
py> C().tot_expenses(110, 24)
(110, 24)
py> C().tot_expenses(110, 24, 2, 56)
(110, 24, 2, 56)

Steve
Jul 18 '05 #4
And in case it's not obvious already, you get the number of arguments that
got passed down from:

len(args)

"Nick Coghlan" <nc******@iinet.net.au> wrote in message
news:ma*************************************@pytho n.org...
rbt wrote:
How do I set up a function so that it can take an arbitrary number of
arguments? For example, I have a bunch of expenses which may grow or
shrink depending on the client's circumstance and a function that sums
them up... hard coding them is tedious. How might I make this dynamic so
that it can handle any amount of expenses?

def tot_expenses(self, e0, e1, e2, e3):
pass
The Python Tutorial is a wonderful thing. . .

Anyway, you can either set up your function to take a proper list, and

then discover that the sum function already exists to add up the contents of a list:
def tot_expenses(self, expenses):
self.total_expenses = sum(expenses)

Or, have the function take a variable number of arguments, and do the same thing:
def tot_expenses(self, *args):
self.total_expenses = sum(args)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

Jul 18 '05 #5

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

Similar topics

4
by: Magnus Jonneryd | last post by:
I'm trying to figure out if it's possible to implement a function in C++ that has an argument list of arbitrary length. Is this possible (I don't want to use a data structure to accomplish this)? ...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
2
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.