Say you want to pass a dictionary of values to a function, or otherwise want to work with a short-lived dictionary that won’t be reused. There are two easy ways to do this:
Use the dict() function to create a dictionary:
foo.update(dict(bar=42, baz='qux'))
Use an anonymous dictionary:
foo.update({'bar': 42, 'baz': 'qux'})
Which do you prefer? Are there reasons other than personal style for choosing one over the other?
I prefer the anonymous dict option.
I don’t like the
dict()option for the same reason I don’t like:With the
dict()option you’re needlessly calling a function which is adding overhead you don’t need: