# [Python-ideas] Why operators are useful

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Mar 18 17:49:41 EDT 2019

```Rémi Lapeyre wrote:

> You can make "inferences from the way things are used". But the
> comparison with maths stops here, you don’t make such inferences because your
> object must be well defined before you start using it.

In maths texts it's very common to see things like 'Let y = f(x)...'
where it's been made clear beforehand (either explicitly or implicitly)
what type f returns.

That's completely analogous to inferring the type bound to a Python
name from an assignment statement.

> You can track types with
> comments but you need to comment each line.

No, you don't, because most lines in most programs allow types to
be inferred. The reason that things like MyPy are possible and
useful is that Python programs in practice are usually well-typed.

> In maths, an given object is not duck because it quacks and walks like
> a duck, it’s
> either part of the set of all ducks, or not.

But there's no restriction on how you define the set of all ducks.
It could be "the set of all things that quack". Duck typing is
totally possible in mathematics, even common.

For example, in linear algebra the ducks are "anything you can
apply a linear operator to". That can cover a surprisingly large
variety of things.

> But the equation is only meaningful in a given context. Asking whether
> f: x -> 1/x
> is differentiable is only meaningful if we know whether x is in R, C,
> [1; +oo[...

That depends on what you want to say. You can say "let f be a
differentiable function" and then go on to talk about things that
depend only on the differentiability of f, without knowing
exactly what types f operates on. Then later you can substitute
any function you know to be differentiable, and all of those
thing will be true for it.

Mathematicians abstract things this way all the time. Groups,
fields, vector spaces, etc. are all very abstract concepts,
defined only as a set of objects having certain properties.
If that's not duck typing, I don't know what is.

--
Greg

```

More information about the Python-ideas mailing list