Multiple arguments: how do you handle them 'nicely'?

Erik Max Francis max at alcyone.com
Fri Aug 9 01:36:47 EDT 2002


Blair Hall wrote:

> I would be interested to know how others would tackle the following
> simple situation.
> 
> I would like to define a function that will accept an arbitrary number
> of arguments. So,
> for example, I  write:
> 
>     def f(*args):
>         for i in args:
>             print i
	...
> I would prefer that f() behave the same way for either a list or
> tuple,
> or a comma separated
> series of arguments. Moreover, if  f() is passed something that
> emulates
> a sequence type then
> it should handle that too.
> 
> How should I write f() so that it recognizes when it has been passed a
> container
> that is sequence-like and when it simply has a series of arguments?

Why not something like:

	def f(*args):
	    if len(args) == 1:
	        args = args[0]
            for i in args:
	        print i

This presumes, of course, that you want the pass-several-arguments and
pass-one-argument-that-is-a-sequence cases to behave identically.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list