Okay, the Zen doesnât say that there should be Only One Way To Do It. But it does have a prohibition against allowing âmore than one way to do itâ.
There is no such prohibition. The âZen of Pythonâ merely expresses a preference for âonly one obvious wayâ:
There should be one-- and preferably only one --obvious way to do it.
The emphasis here is that there should be an obvious way to do âitâ. In the case of dict update operations, there are at least two different operations that we might wish to do:
Update a dict in place : The Obvious Way is to use the update() method. If this proposal is accepted, the |= augmented assignment operator will also work, but that is a side-effect of how augmented assignments are defined. Which you choose is a matter of taste.
Merge two existing dicts into a third, new dict : This PEP proposes that the Obvious Way is to use the | merge operator.
In practice, this preference for âonly one wayâ is frequently violated in Python. For example, every for loop could be re-written as a while loop; every if block could be written as an if / else block. List, set and dict comprehensions could all be replaced by generator expressions. Lists offer no fewer than five ways to implement concatenation:
Concatenation operator: a + b
In-place concatenation operator: a += b
Slice assignment: a[len(a):] = b
Sequence unpacking: [*a, *b]
Extend method: a.extend(b)
We should not be too strict about rejecting useful functionality because it violates âonly one wayâ.
Zen of Python was never meant to be a rule. Take a look at this post by Lukasz Langa for further details.
Summarizing it in Guidoâs words:
âItâs poetry, not a set of axioms. You canât prove anything with an appeal to PEP 20. You can appeal to it, for sure, but such an appeal by definition is subjective and emotional. (Thereâs Only One Way To Do It? Give me a break. :-)â
The Zen of Python is a joke that has been elevated up to the position of Holy Writ in the minds of some people, but it should not be taken too seriously. Especially not the One Obvious Way koan, which has been explicitly described as a joke by its creator, Tim Peters.
So they definitely arenât rules. They arenât even âpolicyâ. Policy is a pseudo-rule that, except for certain catastrophic situations, provides guidance rather than an edict. Good policy should be based on actual experience rather than attempting to enforce some arbitrary version of an ideal organization. The nature, development, and application of good policy is a whole additional subject.
Theyâre somewhere between philosophy and theory. You find them applied in practice through style guides and coding guidelines and conventions (all of which are forms of policy). A good style guide will have the best practices layered in categories of ârecommendedâ, âdesiredâ, and âmandatoryâ. The pyZen principles are in the ârecommendedâ group and should be self-evident enough to naturally flow into a âdesiredâ status.
The principles are simply expressions of some building blocks of a certain mindset that leads to best practices. Rather than being edicts, theyâre gentle urgings toward a mindfulness that leads to right action. NOTE: this is different from a âkoanâ, which is a phrase thatâs designed to deconstruct your assumptions. They arenât koan any more than theyâre mantra for meditation.
We might want to consider ichigi (Japanese âee-chee-geeâ) or yimu (Chinese âee-mooâ),which both mean first ~principle/~meaning/~reason/~consideration. Koan does have the interesting correlation with âkohenâ (priest), though that tends toward complicated rather than complex or simpleâwhich might be a compelling reason to evolve from âkoanâ in addition to the fact that its definition doesnât truly apply.