[Python-ideas] Let's be more orderly!

Haoyi Li haoyi.sg at gmail.com
Sun May 19 04:13:12 CEST 2013


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:

http://docs.python.org/2/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields

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.

-Haoyi


On Fri, May 17, 2013 at 5:07 AM, Steven D'Aprano <steve at pearwood.info>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):
>>>      pass
>>>
>>> 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).
>
>
>
> --
> Steven
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130518/e614ab2f/attachment.html>


More information about the Python-ideas mailing list