PEP0238 lament

Paul Svensson paul at svensson.org
Mon Jul 23 17:51:29 EDT 2001


"Andy Salnikov" <salnikov at inp.nsk.su> writes:

>Guys, if you really need something returning floats for division, make it
>new operator, I guess everyone could be made happy with 1/2 == 0 and 1//2 ==
>0.5.

There are two common ways to do division.

A) x / y = z such that z * y = x,

B) x / y = z and x % y = r such that z = int(z), 0 <= r < x, z * y + r = x

In current python, (B) is "z, r = divmod(x, y)",
while (A) can be approximated by "(x + 0.0) / y".
Unadorned "x / y" picks (A) or (B) depending on the types of x and y.

Now we have three problems:
1) The current semantics of x/y are confusing and error-prone
2) (x + 0.0) / y is nonintuitive and ugly
3) divmod(x, y)[0] is nonintuitive and ugly

Which problem(s) are we trying to solve ?
No solution to (1) will avoid breaking existing code.

We can solve (2) and (3) by adding _two_ new operators (or functions),
or we can solve all three by changing x/y to always do (A) or always (B),
and adding a new operator for the other.

Andy's proposal for "1//2 = 0.5" solves (2), doing nothing about (1) and (3),
and I find very little merit in such a half-done (1/3 done) solution.

	/Paul



More information about the Python-list mailing list