keyword parameter order

Tim Chase python.list at tim.thechases.com
Sun Nov 18 07:40:30 EST 2007


> I am looking for a way to determine the order of keyword parameters
> passed on to a class method.

I'm fairly certain it's not possible, as how would code like this
behave:

  def my_func(**kwd):
    kwd = OrderedDict(kwd) #magic happens here?
    return do_something(kwd)

  my_dict =  {'hello':42, 'world':3.14159}
  print my_func(**my_dict)

This is regularly used in lots of code.  The ordering of the
contents of my_dict is already lost before the my_func() ever
gets a chance to see it.  And in case one suggests trying to
sniff the source-code for the ordering, it's easy to break with
things like

  my_dict = read_dict_from_file(get_filename_from_user())

where the dict and its source are completely outside the scope of
the code.

The only way around it I see is to force the user to pass in an
ordered dict explicitly:

  def my_func(ordered_dict_of_kwdargs):
    return do_something(ordered_dict_of_kwdargs)
  my_dict = OrderedDict()
  my_dict['hello'] = 42
  my_dict['world'] = 3.14159
  print my_func(my_dict)

-tkc







More information about the Python-list mailing list