[Python-ideas] OrderedDict for kwargs and class statement namespace

Ned Batchelder ned at nedbatchelder.com
Thu Feb 28 22:51:42 CET 2013


On 2/28/2013 11:41 AM, Ethan Furman wrote:
> On 02/28/2013 08:14 AM, Don Spaulding wrote:
>>
>>
>>
>> On Thu, Feb 28, 2013 at 9:48 AM, Antoine Pitrou <solipsis at pitrou.net 
>> <mailto:solipsis at pitrou.net>> wrote:
>>
>>     Le Thu, 28 Feb 2013 09:30:50 -0600,
>>     Don Spaulding <donspauldingii at gmail.com 
>> <mailto:donspauldingii at gmail.com>>
>>     a écrit :
>>     >
>>     > For an example of the "recommended" way to get the ordering of 
>> your
>>     > class attributes:
>> >http://stackoverflow.com/questions/3288107/how-can-i-get-fields-in-an-original-order
>>
>>     This is already possible with the __prepare__ magic method.
>> http://docs.python.org/3.4/reference/datamodel.html#preparing-the-class-namespace
>>
>>
>> Sure.  Case in point: Django has been working around it since at 
>> least python 2.4.
>>
>>     > It seems to me that the "right thing" for python to do when 
>> given an
>>     > ordered list of key=value pairs in a function call or class
>>     > definition, is to retain the order.  So what's an acceptable 
>> level of
>>     > performance regression for the sake of doing things the "right 
>> way"
>>     > here?
>>
>>     Or, rather, what is the benefit of doing things "the right way"? 
>> There
>>     are incredibly few cases for relying on the order of key=value 
>> pairs in
>>     function calls.
>>
>>
>> "If you build it, they will come..."
>>
>> When I originally encountered the need for python to retain the order 
>> of kwargs that my caller specified, it surprised
>> me that there wasn't more clamoring for kwargs being an OrderedDict.  
>> However, since my development timeline didn't
>> allow for holding the project up while a patch was pushed through 
>> python-dev and out into a real python release, I
>> sucked it up, forced my calling code to send in hand-crafted 
>> OrderedDicts and called it a day.  I think most developers
>> don't even stop to think that the language *could* be different, they 
>> just put in the workaround and move on.
>>
>> I think if python stopped dropping the order of kwargs on the floor 
>> today, you'd see people start to rely on the order
>> of kwargs tomorrow.
>
> +1
>
> I'd already be relying on it if it were there.
>

Could you advance the discussion by elaborating your use case?  I've 
never had need for ordered kwargs, so I'm having a hard time seeing how 
they would be useful.

--Ned.

> -- 
> ~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>




More information about the Python-ideas mailing list