1. 13
  1.  

  2. 4

    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.

    1. 5

      It also says:

      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.

      1. 2

        Even defaults | user would have been better, since it doesn’t add new syntax (to be consistent with set)

        1. 1

          Why abusing an operator when a function is more descriptive? It is not like you merge dictionaries everywhere.

          1. 1

            Why abusing an operator when a function is more descriptive?

            I’d say that has been already a trend in Python with list.__add__ and set.__or__.

      2. 1

        on my personal wishlist would be context = defaults.updated_with(user) (precedent exists in the sort/sorted distinction)