News for hackers. Stuff that matters.
Re:Performance Analysis of Python's Dict() and {} by ezo on Monday January 22, 2024 @03:26PM (#39094933)
I’m wonder, who’s thinking that dict() is more readable???
Re:Performance Analysis of Python's Dict() and {} by kevincox on Monday January 22, 2024 @03:37PM (#39095083)
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.
Re:Performance Analysis of Python's Dict() and {} by formerly_proven on Monday January 22, 2024 @03:51PM (#39095262)
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.
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}
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.)
Re:Performance Analysis of Python's Dict() and {} by pwdisswordfishc on Tuesday January 23, 2024 @03:08AM (#39100722)
At this point, just use a dataclass.
Re:Performance Analysis of Python's Dict() and {} by paulddraper on Monday January 22, 2024 @06:31PM (#39097080)
Of course, for anything except string keys, that isn't even an option.
Re:Performance Analysis of Python's Dict() and {} by coldtea on Monday January 22, 2024 @07:27PM (#39097625)
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.

Re:Performance Analysis of Python's Dict() and {} by paulddraper on Monday January 22, 2024 @08:49PM (#39098466)
Is 1 a integer or a string?
Re:Performance Analysis of Python's Dict() and {} by coldtea on Monday January 22, 2024 @10:46PM (#39099388)
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")

Re:Performance Analysis of Python's Dict() and {} by shoo on Monday January 22, 2024 @03:30PM (#39094991)
`{x, y}` is the set containing the two elements `x` and `y`, `{x}` is the set containing only the element `x`, so "surely `{}` is the empty set" !
Re:Performance Analysis of Python's Dict() and {} by pdonis on Monday January 22, 2024 @03:55PM (#39095307)
If Python had had built-in set notation from the start, {} might indeed have been a notation for an empty set instead of an empty dict.

However, Python didn't even have sets at all until version 2.3, and they were in a stdlib module instead of being a built-in type until version 2.6. By that time dict notation was well entrenched.

Re:Performance Analysis of Python's Dict() and {} by empiko on Monday January 22, 2024 @03:57PM (#39095336)
I think it's more readable. It matches with the type name, it is consistent when you define different structures (list, set, dict vs [], set, {}), you can pass it as a function, etc. [] and {} is unnecessary syntax sugar, not to mention how the same parentheses are overloaded when sets are defined.
Re:Performance Analysis of Python's Dict() and {} by locuscoeruleus on Monday January 22, 2024 @03:50PM (#39095254)
I think dict(name=name, age=age) can be definitely be more readable and ergonomical to write than {"name": name, "age": age}.
Re:Performance Analysis of Python's Dict() and {} by mikepurvis on Monday January 22, 2024 @04:21PM (#39095621)
I still miss coffeescript's ability to do dict(@name, @age) though I understand that it creates an unwelcome coupling between the parameter names and their names in the calling scope.

For my part, those names are often the same anyway, though, since the calling scope names are often arbitrary and might as well match the parameter names.

Re:Performance Analysis of Python's Dict() and {} by lights0123 on Monday January 22, 2024 @06:20PM (#39096982)

    {name, age}
does the same in modern JS, assigning the variable to the key with the same name.
Re:Performance Analysis of Python's Dict() and {} by nerdponx on Monday January 22, 2024 @03:34PM (#39095032)
dict() can be useful for enforcing (or indicating to the reader) that keys are strings and valid Python identifiers. {} allows arbitrary keys, as long as the data type is hashable.
Re:Performance Analysis of Python's Dict() and {} by colpabar on Monday January 22, 2024 @03:31PM (#39095008)
You could argue that since `{}` is also technically an empty set, `dict()` is more explicit.
Re:Performance Analysis of Python's Dict() and {} by masklinn on Monday January 22, 2024 @03:47PM (#39095215)
Except `{}` is never an empty set.
Re:Performance Analysis of Python's Dict() and {} by colpabar on Monday January 22, 2024 @03:58PM (#39095346)
> x = set()

> x

set()

Well now I know! However I still think the shared usage of curly braces for dicts and sets could be somewhat confusing.

> x = {1, 2, 3}

> x

{1, 2, 3}

(But maybe no one should listen to me because I don't know how to do code blocks on HN.)

Re:Performance Analysis of Python's Dict() and {} by tczMUFlmoNk on Monday January 22, 2024 @04:56PM (#39096051)
Indent by four spaces. :-)
Re:Performance Analysis of Python's Dict() and {} by pwdisswordfishc on Tuesday January 23, 2024 @03:10AM (#39100729)
The empty set is `{*()}`.
Re:Performance Analysis of Python's Dict() and {} by empiko on Tuesday January 23, 2024 @03:55AM (#39101043)
this is beautiful
Re:Performance Analysis of Python's Dict() and {} by alanwreath on Tuesday January 23, 2024 @01:22PM (#39107362)
never seen

    {*()}
    
but I guess it is equivalent to

    set()
    
I've noticed this in python in another place

    f"{var}"
    
is the same as

    str(var)
    
there was some other place I can't recall also noticing that (at least my perception understands that) Python is trying to kill my code golf tendencies.

That said, it's probably just my own superstition and of course it doesn't remove my premature optimization tendencies...

Re:Performance Analysis of Python's Dict() and {} by wirrbel on Monday January 22, 2024 @03:37PM (#39095080)
less clutter for string-only keys.
Re:Performance Analysis of Python's Dict() and {} by munch117 on Monday January 22, 2024 @04:27PM (#39095700)
I do. dict() costs me less eye strain to recognise out of the corner of my eye.

Why the triple question marks? You could have just asked the question straight.

Re:Performance Analysis of Python's Dict() and {} by pphysch on Monday January 22, 2024 @04:53PM (#39096005)
One place where I prefer dict() is where dicts are used as "nested kwargs", e.g. in Django's ORM for passing default values.

    User.objects.get_or_create(
      username="bob",
      defaults=dict(
        email="[email protected]",
      )
this makes it easy to move fields between `defaults` and `kwargs`. Granted, you could achieve the same isomorphism with ** operator, but less readably IMO.