What happened to “There should be one– and preferably only one –obvious way to do it.”?
If you’re using Python 3.5, thanks to PEP 448, there’s a new way to merge dictionaries:
context = {**defaults, **user}
This is simple and Pythonic
While it probably does the trick, I am rather appalled by cramming features into such syntactic constructs. The whole article could be answered by CPython having a dict.merge(a,b) classmethod. Unfortunately Python does not.
This is simple and Pythonic. There are quite a few symbols, but it’s fairly clear that the output is a dictionary at least.
I’m not sure how it’s fairly clear that the output is a dictionary. { and } have been overloaded in Python for set construction (which makes {} very confusing depending on where your head is), so why would {**defaults, **user} not give me a set? Or error out? To paraphrase Mark Twain, Python is a language that managed to convince people it is simple and now it can be as complex and nonsensical as it wants. The power of a reputation.
What happened to “There should be one– and preferably only one –obvious way to do it.”?
While it probably does the trick, I am rather appalled by cramming features into such syntactic constructs. The whole article could be answered by CPython having a
dict.merge(a,b)classmethod. Unfortunately Python does not.It also says:
I’m not sure how it’s fairly clear that the output is a dictionary.
{and}have been overloaded in Python for set construction (which makes{}very confusing depending on where your head is), so why would{**defaults, **user}not give me aset? Or error out? To paraphrase Mark Twain, Python is a language that managed to convince people it is simple and now it can be as complex and nonsensical as it wants. The power of a reputation.Even
defaults | userwould have been better, since it doesn’t add new syntax (to be consistent withset)Why abusing an operator when a function is more descriptive? It is not like you merge dictionaries everywhere.
I’d say that has been already a trend in Python with
list.__add__andset.__or__.on my personal wishlist would be
context = defaults.updated_with(user)(precedent exists in thesort/sorteddistinction)