How to do *args, **kwargs properly

Paul Rubin http
Tue Oct 11 13:46:51 CEST 2005


Lasse Vågsæther Karlsen <lasse at vkarlsen.no> writes:
> or is the "proper python" way simply this:
> 
> def fn(*values, **options):
>      if "cmp" in options: comparison = options["cmp"]
>      else: comparison = cmp
>      # rest of function here
> 
> and thus ignoring the wrong parameter names?

I don't know about "proper" but it's pretty common.  You could also
use a positional arg:

  def fn(cmp, *values):
     # blah blah

   fn(cmp, 1, 2, 3)     # call the function

You could check for invalid keys a little more concisely than in your
example.  For example with sets:
  def fn(*values, **options):
    if Set(options.keys()) - Set(('cmp')):
      raise error...



More information about the Python-list mailing list