[Python-ideas] Left division operator
Steven D'Aprano
steve at pearwood.info
Sun Jul 17 18:27:30 CEST 2011
Sturla Molden wrote:
> I was wondering if anyone else has felt the need for a leftward division
> operator in Python. That is, y\x in addition to x/y. Thus the operators:
> y \ x
> y \\ x
You have flipped the operator AND flipped the order of the operands,
which makes it confusing to me. I find it easy to reason about this
proposal if I stick to one change at a time.
I presume that x\y will be equivalent to y/x, or 1/(x/y) if x and y are
numbers. Is there a standard mathematical infix operator for this?
Since x/y => x.__truediv__(y), then x\y => x.__rtruediv__(y).
Once only I have wanted something close to this. It wasn't terribly
compelling: I ended up writing the obvious helper function:
def divide_by(x, y):
return y/x
It did cross my mind at the time that it would be neat if this had \ as
the operator, but I've met enough people who get their forward and back
slashes mixed up that I wouldn't want to add to their confusion. So I
would not support using \ as the operator for this.
> Why the statement "y\x" is different from "x/y":
>
> 1. Opposite order of evaluation: This is important if evaluating one of
> the operands has side-effects.
This is ambiguous to me. Taken literally, you mean that the right-hand
operatand (x) will be evaluated before the left-hand operand (y) (the
opposite of normal for Python). Is that what you mean? I don't see any
justification for that. Even the exponentiation operator evaluates terms
from left-to-right although the operator binds more strongly to the right:
>>> def f(x):
... print(x)
... return x
...
>>> f(2) ** f(3) ** f(4)
2
3
4
2417851639229258349412352
As far as I know, right-to-left evaluation would be a major change to
Python.
Or do you mean that the \ operator has the same order of evaluation as
other operators, but instead of writing this:
tmp = denominator() # Evaluate this first, because it has side-effects.
result = numerator()/tmp
you can write this:
result = denominator()\numerator()
I don't think this is important enough to justify a new operator. After
all, can't we say the same thing for every other non-commutative operator?
y = subtrahend() # Evaluate this first, because it has side-effects.
result = minuend() - tmp
Do we need to introduce new "reversed" operators for subtraction,
exponentiation, left and right binary shift, matrix multiplication, even
for addition? (Because a type can define __add__ and __radd__
separately, there is no guarantee that a+b is commutative.)
If not, why single out division?
> 2. In linear algebra, (Y**-1)*X is in general not equal to X*(Y**-1).
> Matrix and vector algebra in fundamental to computer graphics and
> scientific computing.
>
> 3. NumPy solves the lack of "\" operator by having a function
> np.linalg.solve. But using it in matrix expressions has the effect of
> mixing Pythonic infix operators with a Lisp-like prefix operator.
Is this supposed to be a problem that needs fixing?
> Two other issues:
>
> 1. The expressions
> y /= x
> y //= x
> are evaluated in the same order as "y \ x", not "x / y".
>
> 2. The expressions
> y \= x
> y \\= x
> should perhaps be illegal due to implied side-effects on RHS.
Surely y \= x would have the same meaning as all the other augmented
assignments? It should be the same as
y = y\x
That is, if x and y are numbers, y \= x would be equivalent to
y = x/y
I don't see any reason to make that illegal.
--
Steven
More information about the Python-ideas
mailing list