by necovek · 3y ago · 0 comments
This would be very confusing in suddenly giving dicts a half-ordered, half-not semantics in a particular context.
Why wouldn't you simply switch to setting everything through kwargs instead?
Not to mention that this would have a performance penalty for dicts as they would have to keep an ordered set of integer keys in case they are used with **.
Two dicts with same elements not being equal would be surprising and confusing.
I'd use an `OrderedDict` in situations where I have a mapping where the order of elements is significant. `OrderedDict` signals intent much more clearly, and its `__eq__` method cares about order.
Eg. you can't rely on
dict_b_keys = iter(dict_b)
for key in dict_a:
value_a = dict_a[key]
value_b = dict_b[next(dict_b_keys)]
print(f"{value_a} == {value_b}: ", value_a == value_b)
The problem with the proposal is that it will be even stronger: you either require '*' to sort in-place (I assumed that would be even worse and thus unacceptable, so I ignored that case — it seems others believe that to be a more realistic implementation), or require dicts to maintain a half-sorted map of integer keys.The funniest part is that this does not really matter for this discussion: insertion order is pretty much arbitrary when what you need is for key 0 to come first, key 1 to come next...
> Why wouldn't you simply switch to setting everything through kwargs instead?
Not all arguments in Python are passable both ways.
In
def f(a, /): print(a)
`a` may be passed only positionally.
I was demonstrating the existence of such things in general since OP seemed unaware. For the proposal, you'd of course only use it for functions that took more kinds of arguments.
...why?
I don't love this proposal, but `{1:"a", 2:"b", 0:"c"} == {0:"c", 1:"a", 2:"b"}`, and so presumably either would have the same behavior when passed to `def f(foo, bar baz): ...`.
That the runtime would correct the order as part of the splat operation seems implied?
Just imagine passing in a 500k item dict to ** by accident: dicts maintaining sorted state appeared the only sensible implementation to me.
Like the cost of doing that isn't going to be greater than the existing cost of keyword arguments, which can already be arbitrarily ordered!
Like, what about this is more "preposterous" than passing a massive dict to kwargs already?
Sorting of a large collection happening implicitly is too expensive for what would generally be a benign mistake.
additionally, you might be interested to know that the implementation of dicts as ordered was found to be more performant than its predecessor.
Finally, even if they were unordered, there would be no need for ordering in this proposal, which depends on unique integer keys starting at 0.
For the proposed use case, either **kwargs would have to sort a subset of the dict with integer keys to map positional arguments to their, ahem, positions, and then either leave the rest as-is (or sort it altogether), or a dict would have its definition modified to keep this order as items are added.
I dislike both, and I definitely hate implicit sorting happening with **.
>the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.