5 ms·
For string keys it can be as it is `dict(a=1, b=2, c=3)` vs `{"a": 1, "b": 2, "c": 3}`. There is a lot of `"` noise on the latter.
by kevincox 3y ago
For string keys it can be as it is `dict(a=1, b=2, c=3)` vs `{"a": 1, "b": 2, "c": 3}`. There is a lot of `"` noise on the latter.
- formerly_proven 3y ago50% less noise with ‘ though dict is still slightly easier to read; you can also convey that the keys are supposed to be valid identifiers.
- tweakimp 3y agoI wish I could do {a=3, b=2}
- coldtea 3y agoYou can in Javascript - but with the caveat that the keys can only be strings. Without that, if Python allowed it, would it do given: a = "xxx" test_dict = {a=3, b=2} would it take test_dict to mean {"xxx":3, "b":2} or {"a":3, "b":2} ? JS does the latter always, so the variable a is not related to the literal key a which is understood as an unquoted string "a": > a = "xxx" 'xxx' > test_dict = {a:3, b:2} { a: 3, b: 2 } > test_dict["xxx"] undefined > test_dict["a"] 3
- lifthrasiir 3y agoThe assignment operator `=` can never appear in a valid Python expression, so `{a=3, b=2}` should be distinguishable from `{a: 3, b: 2}`. (So does JS, where `{[a]: 3, [b]: 2}` would evaluate a and b.)
- pwdisswordfishc 3y agoAt this point, just use a dataclass.
- paulddraper 3y agoOf course, for anything except string keys, that isn't even an option.
- coldtea 3y agoNo great reason it couldn't be though: dict(1=5, 6=7, 7="aaa") dict(hashableInstance="foo", anotherHashableInstance=23, (1,5)=8) except Python syntax choices of course.
- paulddraper 3y agoIs 1 a integer or a string?
- coldtea 3y agoIt's an integer (in the, not supported, format I show as something Python could have supported). And if they wanted a string, they'd do it like: dict("1"=5, 6=7, 7="aaa")