allow line break at operators

Chris Angelico rosuav at gmail.com
Mon Aug 15 10:42:08 EDT 2011


On Mon, Aug 15, 2011 at 3:28 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> And of course, once you start using floating point numbers, you can't assume
> commutativity:
>
>>>> 0.1 + 0.7 + 0.3 == 0.3 + 0.7 + 0.1
> False
>

This isn't because programming languages fail to follow mathematics;
it's because floating point numbers do not represent real numbers.
Python doesn't support substring removal using the subtraction
operator, but I'd have to say that floats more closely parallel
strings and other high level objects than they do mathematical reals.
If Python treated __sub__(str,str) as str.replace(str,"") then:


>>> "hello world" + "asdfqwer" - "d"
"hello worlasfqwer"
>>> "hello world" - "d" + "asdfqwer"
"hello worlasdfqwer"

Nobody would expect strings to behave mathematically with subtraction,
because negatives don't make sense. Even sets don't quite work,
although they're closer:

>>> set("asdf")-set("test")
{'a', 'd', 'f'}

There's no way, in a set, to show a negative reference to 't' and 'e'.
In theory you could do this with dictionaries or collections.Counter,
but subtracting a Counter from a Counter doesn't produce negative
numbers either. No, these constructs do not subtract algebraically,
and I do not think it would be any improvement to the language if they
did.

ChrisA



More information about the Python-list mailing list