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

Blair Hall b.hall at irl.cri.nz
Fri Aug 9 06:48:25 CEST 2002


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

Now for a sequence of discrete arguments like:
>>> f(1,2,3)
1
2
3
>>>

But for a list or tuple (which might be the result of another function's
return value)
>>> f( [1,2,3] )
[1,2,3]

and
>>> f( (1,2,3) )
(1,2,3)

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?








More information about the Python-list mailing list