Clean Singleton Docstrings

Steven D'Aprano steve at pearwood.info
Sat Jul 16 04:54:31 EDT 2016


On Sat, 16 Jul 2016 04:53 pm, Random832 wrote:

> On Sat, Jul 16, 2016, at 02:29, Chris Angelico wrote:
>> The difference between ints and floats can lead to bugs, too. Which
>> one should we eliminate?
> 
> Eliminate both of them. Move to a single abstract numeric type* a la
> Scheme, with an "inexact" attribute (inexact numbers may or may not be
> represented by a float, or by the same bigint/decimal/rational types as
> exact ones with a flag set to mark them as inexact.)
> 
> *which may have multiple concrete representations, just as our single
> abstract unicode string type has different concrete representations for
> ASCII, Latin-1, UCS-2, and UCS-4.

No, that's not going to work. It works for Unicode because the
implementation *really is just a detail*. Whether 'A' is represented as a
seven-bit ASCII code, 8-bit Latin-1 code, 16-bit UCS-2 code or 32-bit UCS-4
code makes no difference to the behaviour of the string. (There may or may
not be performance differences, but that's a separate issue.)

But that doesn't hold for numbers. For a combination of accidental,
intentional and historical reasons, we need to support multiple numeric
types with different behaviour sometimes even different APIs. For example,
different int types behave differently on overflow; floats have signed
zeroes and infinities but fractions don't, etc. But most importantly, one
needs to be able to distinguish between (say) real numbers and complex
numbers, where sqrt(x) and sqrt(x+0j) do not necessarily return the same
value. Or you want to implement (say) "integers modulo 12" such that 11+1
gives 0. You can't do that if you are limited to a single abstract Number
type.


Still, there's probably a lot we could do to improve the ability to
duck-type numbers in Python. For instance, it is a nuisance that we write:

  math.isfinite(x)

for floats, but

  x.is_finite()

for Decimals.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list