Creating variables on the fly...

Mike Fletcher mfletch at tpresence.com
Mon Apr 3 15:20:43 EDT 2000


Here's a few ways...  Note the *args construct... also note that you could
use **namedargs to indicate arguments which are passed explicitly by name.
*args gets a tuple of values in the order passed, **namedargs would get a
dictionary mapping name:value.

PythonWin 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Portions Copyright 1994-1999 Mark Hammond (MHammond at skippinet.com.au)
>>> def add( *args ):
... 	return reduce( lambda x,y: x+y, args )
... 
>>> add( 2,3,4,5,6,7)
27
>>> import operator
>>> def add2( *args ):
... 	return reduce( operator.add, args )
... 
>>> add2( 2,3,4,5,6,7)
27
>>> import Numeric
>>> Numeric.sum( (2,3,4,5,6) )
20
>>> 

HTH,
Mike




More information about the Python-list mailing list