Does Python support object literal property value shorthand, a la ECMAScript 6? Does Python support object literal property value shorthand, a la ECMAScript 6? python python

Does Python support object literal property value shorthand, a la ECMAScript 6?


No, but you can achieve identical thing doing this

record = {i: locals()[i] for i in ('id', 'name', 'email')}

(credits to Python variables as keys to dict)

but I wouldn't do it because it compromises readability and makes static checkers incapable of finding undefined-name error.

Your example, typed in directly in python is same as set and is not a dictionary

{id, name, email} == set((id, name, email))


No, there is no similar shorthand in Python. It would even introduce an ambiguity with set literals, which have that exact syntax:

>>> foo = 'foo'>>> bar = 'bar'>>> {foo, bar}set(['foo', 'bar'])>>> {'foo': foo, 'bar': bar}{'foo': 'foo', 'bar': 'bar'}


You can't easily use object literal shorthand because of set literals, and locals() is a little unsafe.

I wrote a hacky gist a couple of years back that creates a d function that you can use, a la

record = d(id, name, email, other=stuff)

Going to see if I can package it a bit more nicely.