3 ms·
>Does the author not realize that you can use (almost) anything as a dict key? You can do that in JS too, it's just coerced to string, so you can do e.g. {f
by coldtea 7y ago
>Does the author not realize that you can use (almost) anything as a dict key?
You can do that in JS too, it's just coerced to string, so you can do e.g.
{foo: "bar"}
where foo is not a variable, but assumed to be the string foo. In Python you can't, because you couldn't tell if it's foo the string, or a reference to an existing (or non-existing) foo variable.
That said, in JS, not only you're limited to using (real or coerced) strings as keys to Objects, but you also need to use the square bracket:
{[foo]: "bar"}
syntax, so it can tell that you need it to use the value of the variable foo as the key (else it will understand {"foo": "bar"}).
- TylerE 7y agoThat's different though. In python there is no coercion going on with the keys.