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

Gonçalo Rodrigues op73418 at mail.telepac.pt
Fri Aug 9 14:08:16 CEST 2002


On Thu, 08 Aug 2002 22:52:38 -0700, Erik Max Francis <max at alcyone.com>
wrote:

>Erik Max Francis wrote:
>
>> Why not something like:
>> 
>>         def f(*args):
>>             if len(args) == 1:
>>                 args = args[0]
>
>Err, that's obviously wrong.  I obviously meant
>
>	      if len(args) == 1 and \
>                 type(args[0]) in (types.ListType, types.TupleType):
>	           ...
>
>Although obviously that doesn't take into account arbitrary sequences. 
>In that case a reasonable test might be to try to do len(args[0]), catch
>the error if it's not a sequence type, and go from there.

Even better, after

if len(args) == 1

use  a try/except block to see if you are passed an iterable, e.g.

try
    it = iter(args)
    <whatever>
#Not an iterable - proceed acordingly.
except TypeError:
    <whatever>

With my best regards,
Gonçalo Rodrigues



More information about the Python-list mailing list