Warning: serious linguistic hacking follows. I tried to be careful in writing, please be careful in reading. Corrections welcome.
Ethan Furman writes: > On 4/9/22 21:17, Stephen J. Turnbull wrote: > > > if 12*u.mm * 42*u.MFr == 502*u.foo: > > print('Well done!') > > Part of the argument as well, I think, is that the top expression > would be parsed as: > > ((12 * u.m) * 42) * u.MFr > > which, if actually equal to 502*u.foo, is dumb luck. It's actually equal to 504*u.foo, I shouldn't do multiplications larger than 12 * 12 in my head. But yes, the correct answer is (12 * 42) * u.foo, if I could only do arithmetic on integers! I guess you could call the associative law of multiplication "dumb luck", but most mathematicians will consider that hate speech. If the unit class is designed correctly (the units package is designed correctly), both the associative and commutative laws of multiplication hold. The only gotcha as far as the value is concerned is that if you put in divisions such as u.m/u.s, then you often will need parentheses. But that's true for ordinary arithmetic as well. There would be a problem if the LHS were 12*u.mm / 42*u.MFr. That's actually nonsense in a units-aware world. Parentheses are required: 12*u.mm / (42*u.MFr). The "we demand syntax" crowd wants the "add unit" operation to have higher precedence than numerical multiplication and division. Hmm, unfortunately both '@' and '%' have the same precedence as '*' and '/', but we could make the precedence more natural by using '**' at the cost of the intuition that a unit is just a quantity (object with both value and unit attributes) with value 1. I'm not sure that intuition helps the "we demand syntax" crowd, though. Another way to put the issue is that libraries like units don't provide units, they provide quantities. You can create a quantity foo that acts like a unit by giving it the value 1, but 10*foo is of the same type as foo. It just has value attribute 10. Again, that doesn't bother me, but I suspect that the "we demand syntax" crowd will feel queasy about the idea that 'nm' *is* '1 nm'. For them, 'nm' needs to be notated with a value to be a quantity. I don't want to put words in anybody's mouth. If Brian or Rickey disclaims that interpretation, they're right. If they think it has expressive value but needs to be amended, they're right. I'm just trying to provide words to express what the need is here. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MQW2YFN7I2ATZV2AOPHVKLW2H6EU3FCI/ Code of Conduct: http://python.org/psf/codeofconduct/