keyword parameter order

Anthon Anthon.van.der.Neut at googlemail.com
Sun Nov 18 11:30:28 CET 2007


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

In the source code the keyword parameters are ordered, an ordering
that is lost by putting them into a dictionary and then accessing them
by using **kw. If I had this order (either of the keyword+value pairs
or just of the keywords) I could meaningfully initialise my
ordereddict implemenation (which uses Key Insertion Order or KeyValue
Insertion Order).

I looked at traceback (which shows this info if all the keywords are
on one source line), and tracing that I found that it displays the
sourcecode line based on the code-object found in the frame stack. I
could persue that but I find the idea of reparsing the sourcecode kind
of ugly (although doable, as the keyword are never variables, and I
would not have to reevaluate the variables).

I am not sure if this kind of info is available internally to the
interpreter (ordereddict is in C, so I would even prefer that). Has
anyone done this or anything like it?

I could probably compile the python interpreter to use ordereddict
instead of dict and then the parser would insert the keyword
parameters in specification order in the **kw ordereddict, after which
iteration over parameters would be ordered. I am pretty sure though if
the 10% speed overhead of the ordereddict implementation compared to
dict is going to prevent people from using such an interpreter
version.

As an aside: I think that allowing dict to be initialised from keyword
parameters (introduced in Python 2.2 IIRC) was a mistake. This kind of
use of keyword parameters prevents any real keyword parameters. E.g.
with 'maxsize' that restricts the dictionary size or 'unique' that
would throw an Exception if an existing key gets reassigned.

Anthon



More information about the Python-list mailing list