Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


50% less noise with ‘ though dict is still slightly easier to read; you can also convey that the keys are supposed to be valid identifiers.


I wish I could do {a=3, b=2}


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


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.)


At this point, just use a dataclass.


Of course, for anything except string keys, that isn't even an option.


No 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.


Is 1 a integer or a string?


It'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")




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: