keyword parameter order

Anthon Anthon.van.der.Neut at googlemail.com
Sun Nov 18 14:00:06 EST 2007


Hi Tim,

Thanks for the comments, I obviously hadn't thought beyond the simple
case.
I am happy I wrote (and that you Martin answered) instead of trying to
program myself into a halffunctional implementation %-)

Regards
Anthon

On Nov 18, 1:40 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> > 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