Re:Performance Analysis of Python's Dict() and {}
by tweakimp on Monday January 22, 2024 @04:15PM
(#39095569)
I wish I could do {a=3, b=2}
[ Parent
| on HN ]
Re:Performance Analysis of Python's Dict() and {}
by coldtea on Monday January 22, 2024 @07:32PM
(#39097664)
You 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
Re:Performance Analysis of Python's Dict() and {}
by lifthrasiir on Tuesday January 23, 2024 @11:07PM
(#39113504)
The 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.)