[Python-ideas] Why operators are useful

Terry Reedy tjreedy at udel.edu
Sat Mar 16 04:40:44 EDT 2019

I agree with Guido's general comments on operators.  Modern arithmetic 
and algebra really took off with the introduction of operators.  On the 
other hand, I have seen condensed blocks of 'higher math', dense with 
operators, that I could hardly read, and that reminded me of API or Perl.

On 3/15/2019 9:39 PM, Raymond Hettinger wrote:

> We don't design Python for ourselves. We design it for everyday users. Telling them that they can assume nothing is an anti-pattern. People do rely quite a bit on their intuitions. They also rely on implicit patterns already present in the language (i.e. in no other place is + idempotent, in no other place is + a destructive rather than concatenative or accumulative operator).  As for commutativity, + would be obviously commutative for numeric types and obviously noncommutative for sequence concatenation, but for dicts the non-commutativity isn't obvious at all. And since the "|" operator is already used for mapping views, the + operator for merging would be unexpected.

I agree with this argument in favor of '|' over '+'.

> What is missing from the discussion is that we flat out don't need an operator for this.

I grepped idlelib's 60 modules for '.update('.  Ignoring the tkinter 
.update() calls, there are 3 uses of copy-update, to create a namespace 
for eval or exec, that could use the new operator.  There are 3 other 
used to update-mutate an existing dict, which would not.

If someone took a similar look as stdlib modules, I missed it.  So I 
looked at non-package top-level modules in /lib (no recursion).  The 
following likely has a few mis-classification mistakes, but most were clear.

35 dict mutate updates
7 set updates
8 dict copy-updates that could use '|' (assuming not set updates)
   # I did not think of set possibility until I had seen move of these
4 copy, intervening try or if, update
   # these either could not use '|' or only with code contortion

5 tk widget updates
10 other update methods  (a few 'dict updates might belong here)
10? 'update's in docstrings and comments
79 hits

Terry Jan Reedy

More information about the Python-ideas mailing list