Re: [Python-Dev] Symmetry arguments for API expansion

2018年3月21日 16:08:23 -0700

On Wed, Mar 21, 2018 at 09:46:06AM -0700, Nathaniel Smith wrote:
[...]
> For me this is an argument against is_integer() rather than for it :-).
> is_prime(float) should *obviously*[1] be a TypeError. Primality is only
> meaningfully defined over the domain of integers
And 3.0 is an integer. Just because it is float *object* does not mean 
it is not an integer *value*. Do not mistake the leaky abstraction of 
multiple numeric types for the mathematical number three.
Primality-related functions are not limited to integers. For example, 
the prime counting function is defined on the reals:
https://en.wikipedia.org/wiki/Prime-counting_function
and there's no reason not to extend the domain of is_prime to any real. 
"Practicality beats purity" -- why should the result be different just 
because the input has a ".0" at the end?
Mathematically it doesn't: the answer to something like "Is 3.0 a 
prime?" is a clear Yes, not "I'm sorry, I don't understand the 
question!" which an exception would imply.
As programmers, there is always a tension between the leaky abstraction 
of our numeric types, and the mathematical equality of:
 3 == 3.0 == 9/3 == 3+0j
etc. The decision on whether to be more or less restrictive on the 
*types* a function accepts is up to the individual developer. Having 
decided to be *less* restrictive, an is_integer method would be useful.
For what it's worth, Wolfram|Alpha gives inconsistant results. It allows 
testing of rationals for primality:
 "Is 9/3 a prime?"
evaluates as true, but:
 "Is 3.0 a prime?"
gets parsed as "Is 3 a prime number?" and yet evaluates as false. A 
clear bug for software using a natural-language interface and intended 
to be used by students and non-mathematicans.
> and this is a case where
> operator.index is exactly what you want.
It is exactly not what I want. 
> Of course it's just an example, and perhaps there are other, better
> examples. But it makes me nervous that this is the best example you could
> quickly come up with.
I actually had to work hard to come up with an example as simple and 
understandable as primality testing. The first example I thought of was 
Bessel functions of the 1st and 2nd kind with arbitrary real-valued 
orders, where you *absolutely* do want order 3.0 (float) and order 3 
(int) to be precisely the same.
But I avoided giving it because I thought it would be too technical and 
it would intimidate people. I thought that the prime number example 
would be easier to understand.
Next time I want to make a point, I'll go for argument by intimidation.
*wink*
-- 
Steve
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to