<div dir="ltr">><span style="font-family:arial,sans-serif;font-size:13px">The question is, how would you _specify_ that option?</span><div><br></div><div>This seems like the perfect use case for function annotations, or a decorator. I imagine both cases would look rather pretty</div>
<div><br></div><div><div><font face="courier new, monospace">def func(**kwargs: ordered):</font></div><div><font face="courier new, monospace"> ...</font></div></div><div><font face="courier new, monospace"><br></font></div>
<div style><font face="courier new, monospace">@ordered_kwargs</font></div><div style><font face="courier new, monospace">def func(**kwargs):</font></div><div style><font face="courier new, monospace"> ...</font></div>
<div style><br></div><div style>I'm not sure if this is a bad idea for other reasons (e.g. decorators/annotations being reserved for library code rather than core language features) but it does look right intuitively: you are annotating the function or the kwarg to change it's behavior.</div>
<div style><br></div><div style>Here's another thought: <a href="https://github.com/lihaoyi/macropy">macros</a> would be able to pretty trivially give you a syntax like:</div><div style><br></div><div style><font face="courier new, monospace">>>> odict(a = 1, b = 2)</font></div>
<div style><font face="courier new, monospace">OrderedDict([('a', 1), ('b', 2)])<br></font></div><div style><div><font face="courier new, monospace">>>> odict(b = 2, a = 1)</font></div><div><font face="courier new, monospace">OrderedDict([('b', 2), ('a', 1)])</font></div>
<div><font face="courier new, monospace"><br></font></div></div><div style>or</div><div style><br></div><div style><font face="courier new, monospace">>>> o%dict(a = 1, b = 2)</font></div><div style><font face="courier new, monospace">OrderedDict([('a', 1), ('b', 2)])<br>
</font></div><div style><div><font face="courier new, monospace">>>> o%dict(b = 1, a = 1)</font></div><div><font face="courier new, monospace">OrderedDict([('b', 2), ('a', 1)])</font></div></div>
<div style>
<br></div><div style>for ordered dict literals. It wouldn't work for generally adding orderliness to other functions, since the macro won't know which bindings are named arguments and which bindings are **kwargs, but it also looks really pretty. Probably not something you'd want to put in the std lib, but it's fun if you want to try out the syntax in your own projects.</div>
<div style><br></div><div style>-Haoyi</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, May 21, 2013 at 12:40 PM, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div class="im"><div>On May 18, 2013, at 19:13, Haoyi Li <<a href="mailto:haoyi.sg@gmail.com" target="_blank">haoyi.sg@gmail.com</a>> wrote:</div>
<div><br></div><blockquote type="cite"><div><div dir="ltr">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? </div>
</div></blockquote><div><br></div></div><div>The question is, how would you _specify_ that option?</div><div><br></div><div>The best way I can think of is a function attribute, with a decorator to set the attribute. Similar to my earlier suggestion for a function attribute that takes a constructor callable.</div>
<div><br></div><div>Your idea is simpler conceptually, but it's not much simpler to use, and it's actually more complicated in implementation. </div><div><br></div><div>The existing function calling machinery explicitly uses mapping functionality, at least in CPython and PyPy. Not that it would be _hard_ to rewrite it around a sequence instead, but it would still be harder than not doing so.</div>
<div class="im"><br><blockquote type="cite"><div><div dir="ltr">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:<div>
<br></div><div><a href="http://docs.python.org/2/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields" target="_blank">http://docs.python.org/2/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields</a><br>
</div><div><br></div><div>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.</div>
</div></div></blockquote><div><br></div></div><div>But you're forgetting that the all existing kwargs code would get slower if we first built a list of pairs and then constructed a dict from it, as would any new code that wants to do lookup by name. So, you're slowing down the 90% case to speed up the 10% case.</div>
<div><br></div><div>Also, the existing functionality is something like this pseudocode:</div><div><br></div><div> kwargs = dict(starstarargs)</div><div> for arg, val in zip(namedargs, namedvals):</div><div> if arg not in f.paramnames:</div>
<div> kwargs[arg] = val</div><div><br></div><div>(I linked to the actual CPython and PyPy code earlier in the thread.)</div><div><br></div><div>So, if performance actually matters, presumably you're going to hash the names anyway to do that in check, at which point the biggest cost of using a dict is already incurred.</div>
</div></blockquote></div><br></div>