[Python-ideas] Why operators are useful

Chris Angelico rosuav at gmail.com
Fri Mar 15 22:40:14 EDT 2019

On Sat, Mar 16, 2019 at 1:27 PM Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> > On Mar 15, 2019, at 6:49 PM, Chris Angelico <rosuav at gmail.com> wrote:
> >
> > On Sat, Mar 16, 2019 at 12:40 PM Raymond Hettinger
> > <raymond.hettinger at gmail.com> wrote:
> >> Also, it seems like the efficiency concerns were dismissed with hand-waving. But usually, coping and updating aren't the desired behavior. When teaching Python, I like to talk about how the design of the language nudges you towards fast, clear, correct code.  The principle is that things that are good for you are put within easy reach. Things that require more thought are placed a little further away.  That is the usual justification for copy() and deepcopy() having to be imported rather than being builtins.  Copying is an obvious thing to do; it is also not usually good for you; so, we have you do one extra step to get to it.
> >>
> >
> > I'm not sure I understand this argument. Are you saying that d1+d2 is
> > bad code because it will copy the dictionary, and therefore it
> > shouldn't be done? Because the exact same considerations apply to the
> > addition of two lists, which already exists in the language. Is it bad
> > to add lists together instead of using extend()?
> Yes, that exactly.

Okay, fair. Though that doesn't necessarily push people towards
operators. Your example from below:

>         blocks += [block]                   # Normally done with append()
>         blocks = blocks + [block]              # Not good for you.

contrasts two different ways of using operators, not operators vs
methods (and as you say, the "good" example is more usually spelled
with a method anyway). So I'm not sure what this means in terms of
dictionary merging.

I'm in favour of having both "merge to new" and "merge into this"
operations (spelled as either + and +=, or | and |=, and I'm not
fussed which of those is picked). As with everything else, "x += y"
can be assumed to be the better option over "x = x + y", but the
difference between copy/update and in-place update is the job of
augmented assignment, not an operator/method distinction.

> Consider a table in a database. Usually what people want/need/ought-to-do is an SQL UPDATE rather than copy and update which would double the memory requirement and be potentially many times slower.

(Heh. Funny you mention that example, because PostgreSQL actually
implements updates by copying a row and then marking the old one as
"will be deleted by transaction X". But that's unrelated to this, as
it's a design decision for concurrency.)

So in terms of "design pushing people to the performant option", the
main takeaway is that, if dict addition is implemented, augmented
addition should also be implemented. I don't think that's really under
dispute. The question is, should addition (or bitwise-or, same diff)
be implemented at all? Performance shouldn't kill that.


More information about the Python-ideas mailing list