Creating variables on the fly...

Moshe Zadka moshez at math.huji.ac.il
Mon Apr 3 18:33:20 EDT 2000


On Mon, 3 Apr 2000, Matthew Hirsch wrote:

> But now let's say I have twenty variables that I want to add together.  
> This function will no longer work.  I would have to rewrite it as 
> a+b+c+d+e+f+g+...+(20th letter).  Is there a way to dynamically create a 
> variable name?  So that my add function can automatically determine how 
> many variable names to create and then add the values together.  In 
> other words, I'd ideally like something like:

Try:

def sum(*args):
	res = 0
	for arg in args:
		res = res+arg
	return res

--
Moshe Zadka <mzadka at geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list