[Python-ideas] SI scale factors in Python

Chris Angelico rosuav at gmail.com
Thu Aug 25 02:03:07 EDT 2016


On Thu, Aug 25, 2016 at 3:57 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> If units are retained, what you have is no longer a simple number, but
>> a value with a unit, and is a quite different beast. (For instance,
>> addition would have to cope with unit mismatches (probably by throwing
>> an error), and multiplication would have to combine the units (length
>> * length = area).)
>
>
> And that can be surprisingly tricky. For example, newtons
> times metres equals joules -- but *only* if the force and
> the distance are in the same direction, otherwise it's
> torque rather than energy and the units are just
> newton-metres.
>

Yeah. And a full-on unit-aware numeric system doesn't belong in the
core language IMO. It belongs on PyPI, with an API like:

length = N("100m")
width = N("50m")
area = length * width
depth = N('2"') # inches
volume = area * depth
time = N("5 hours")
flow_rate = volume/time
print("Rain flowed through the pipe at", flow_rate)

No core language changes needed for that. And since, in most cases,
the values will come from user input anyway, a literal syntax won't be
as important.

ChrisA


More information about the Python-ideas mailing list