5 ms·
I’m wonder, who’s thinking that dict() is more readable???
by ezo 3y ago
I’m wonder, who’s thinking that dict() is more readable???
- shoo 3y ago`{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" !
- pdonis 3y agoIf 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.
- colpabar 3y agoYou could argue that since `{}` is also technically an empty set, `dict()` is more explicit.
- masklinn 3y agoExcept `{}` is never an empty set.
- colpabar 3y ago> 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.)
- tczMUFlmoNk 3y agoIndent by four spaces. :-)
- pwdisswordfishc 3y agoThe empty set is `{*()}`.
- matusp 3y agothis is beautiful
- alanwreath 3y agonever 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...
- nerdponx 3y agodict() 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.
- wirrbel 3y agoless clutter for string-only keys.
- kevincox 3y agoFor 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.
- locuscoeruleus 3y agoI think dict(name=name, age=age) can be definitely be more readable and ergonomical to write than {"name": name, "age": age}.
- mikepurvis 3y agoI 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.
- lights0123 3y ago{name, age} does the same in modern JS, assigning the variable to the key with the same name.
- matusp 3y agoI 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.
- munch117 3y agoI 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.
- pphysch 3y agoOne 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="bob@example.com", ) this makes it easy to move fields between `defaults` and `kwargs`. Granted, you could achieve the same isomorphism with ** operator, but less readably IMO.