passing/catching a variable number of args in python

Steven D. Majewski (sdm7g@aemsun.med.Virginia.EDU)
Wed, 4 Dec 91 17:45:05 EST

Arguments are passed as tuples, except in the case where there is one
or zero arguments.
f( 1,2,3 ) *is the* f( (1,2,3) )
f( 1,2 ) *same as* f( (1,2) )
*BUT*
CASE 1: f( 1 ) or f( (1) ) *is NOT* f( 1, ) or f( (1,) )
*nor is*
CASE 0: f( ) *equiv.to* f( () )

This works to pass a variable number of arguments, except in the
case of NO arguments, which generates an error. Is there any way
around this other than wrapping a try:except: around it to catch
the type error for zero arguments ?

>>> def who( x ):
... print type( x )
... if type(x) <> type( () ): #this takes care of CASE 1
... x = x,
... print len(x)
... print x
...
>>> who( 1,2,3 )
<type 'tuple'>
3
(1, 2, 3)
>>> who( 1, ) # CASE 1: tuple of len = 1
<type 'tuple'>
1
(1,)
>>> who( 1 ) # CASE 1:
<type 'int'> # coerced into a tuple.
1
(1,)
>>> who( () ) # CASE 0:
<type 'tuple'>
0
()
>>> who( )
Unhandled exception: type error: function expects argument(s)
Stack backtrace (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1

======== "If you have a hammer, find a nail" - George Bush,'91 =========
Steven D. Majewski University of Virginia Physiology Dept.
sdm7g@Virginia.EDU Box 449 Health Sciences Center
Voice: (804)-982-0831 1600 Jefferson Park Avenue
FAX: (804)-982-1616 Charlottesville, VA 22908