<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On May 18, 2013, at 19:13, Haoyi Li <<a href="mailto:haoyi.sg@gmail.com">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>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><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">http://docs.python.org/2/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields</a><br>
</div><div><br></div><div style="">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>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></body></html>