Forgive me if this has been mentioned before (i don't think it has) but how about an option somehow to take the list of **kwargs as an association-list? I am approaching this from a point of view of "why am I putting everything into a hashmap just to iterate over it later", as you can see in the way the namedtuple constructor is implemented:

This may be rather out-there, and I'm not sure if it'll speed things up much, but I'm guessing iterating over an assoc list is faster than iterating over anything else. Building an assoc list is also probably faster than building anything else and it's also the most easily convertible (either to OrderedDict or unordered dict) since it preserves all information.


On Fri, May 17, 2013 at 5:07 AM, Steven D'Aprano <> wrote:
On 16/05/13 23:50, Ethan Furman wrote:
On 05/15/2013 08:16 PM, Steven D'Aprano wrote:

I don't believe it can. Hence, when order is important, you cannot use keyword arguments to provide arguments *even if
kwargs are ordered*. But if you write your function like this:

def create_element(tag, mapping):

and call it like this:

create_element('img', OrderedDict([('alt', 'something'), ('src', 'something.jpg')]))

then you can get order for free. Yes, it's a little less convenient to use a list of tuples than nice keyword syntax,
but that's a solution that doesn't impose any costs on code that doesn't care about ordering.

Which 'free' are you talking about?  Because if the solution requires extra typing and extra visual clutter, it's not free.

Free like a puppy :-)

You make a good point. Perhaps "free" was a bad choice of words. Rather, let me say that if you need ordered keyword arguments, you can have them *right now* without waiting for the day when you can drop support for everything older that Python 3.4 (or whatever version gives you order-preserving kwargs).


Python-ideas mailing list