The docs for Plotly and many answers on Stack Overflow trend toward using
dict(foo=bar)
instead of
{'foo':'bar'}
Is there a particular reason for this preference? I've been told in the past that curly brace initialisation is preferred.
Some examples pulled randomly:
One reason why dict(foo=bar)
may be preferred to {'foo':'bar'}
:
In {'foo':'bar'}
, the key foo
can be initialized to be any string. For example,
mydict = {'1+1=':2}
is allowed.
dict(foo=bar)
ensures the dictionary keys to be valid identifiers. For example,
mydict = dict('1+1='=2)
will return error SyntaxError: keyword can't be an expression
.
The second reason may be when you want the key to be not a string. For example, dict(a = 2)
is allowed, but {a: 2}
is not. You would want {'a': 2}
.