5 ms·
I kinda dig the new mapping syntax: { a:, b: } #=> { a: 1, b: 2 } JS does it { a, b } and I find it very convenient, because if you code well, you usually
by BiteCode_dev 5y ago
I kinda dig the new mapping syntax:
{ a:, b: } #=> { a: 1, b: 2 }
JS does it { a, b } and I find it very convenient, because if you code well, you usually have related things with the same name and repeating the name twice is just boilerplate.
Because of the set syntax in Python, the JS syntax can't work, but the ruby one would be nice.
Since Python keys can be anything, but usually are strings (and hence are quoted), I would probably invert the ":" though:
{ :a, :b }
- nicoburns 5y agoI really like the ruby syntax. JavaScript now has the problem that it can't make code blocks expressions because it's ambiguous with object literal shorthand.
- tln 5y agoJavaScript has labels, so objects have always been ambiguous with blocks
- BiteCode_dev 5y agoPython will never have block expression, given how limited lambda is already, so that's not a concern.
- lysecret 5y agothere is no way to achieve this in Python right? I miss this from js.
- BiteCode_dev 5y agoNope.
- dec0dedab0de 5y agoSo its a shortcut that if you pass a variable as a key with no value it automatically creates a key with the same name as the variable and assigns the value? That is kind of neat, but I tend to think of a bunch of {'a':a} as a bit of a code smell. Like maybe I should have used the hash/object/dict to begin with
- BiteCode_dev 5y agoIt's mostly useful when a function generates values then return them as a dict or when things are parts in different chained calculations before being included in the dict to save lookups.
- bobbylarrybobby 5y agoIt's not just because of the set syntax; it's also because Python dicts don't treat their keys as symbols, but rather evaluate them as expressions, whereas JS evaluates keys as symbols by default and requires `{[key]: val}` to evaluate them. So in Python you'd have to do something funky where `{a: a}` is `{value_of_a: value_of_a}`, but, say, `{a:}` is `{"a": value_of_a}`.