How to do *args, **kwargs properly

George Sakkis gsakkis at rutgers.edu
Tue Oct 11 14:52:36 CEST 2005


"Lasse Vågsæther Karlsen" <lasse at vkarlsen.no> wrote:

> So what you're saying is that instead of:
>
> def fn(*values, **options):
>
> I should use:
>
> def fn(values, cmp=cmp):
>
> in this specific case?
>
> and then instead of:
>
> fn(1, 2, 3, cmp=...)
>
> this:
>
> fn([1, 2, 3], cmp=...)
>
> <snip>
>
> I think I'll re-write to use a list instead

Actually in most cases you don't need to assume it's a list; any
iterable is usually good enough. You can always turn it into a list (or
a tuple or a set or..) in the function if you really have to. So when
you do have to and when you don't ? You don't have to if all you do is
iterate over the elements. This is true even if you want to iterate
more than once; just use the itertools.tee() function to create N
independent iterators. To sum up, a better signature for your function
is likely to be "def fn(iterable, cmp=cmp)".

George




More information about the Python-list mailing list