[Python-ideas] SI scale factors in Python

Steven D'Aprano steve at pearwood.info
Thu Aug 25 20:14:53 EDT 2016


On Thu, Aug 25, 2016 at 11:02:11AM -0700, Ken Kundert wrote:

> Once you 
> allow SI scale factors on numbers, the natural tendency is for people to want to 
> add units, which is a good thing because it gives important information about 
> the number. We should allow it because it improves the code by making it more 
> self documenting. Even if the language completely ignores the units, we have 
> still gained by allowing the units to be there, just like we gain when we allow 
> user to add comments to their code even though the compiler ignores them.

This is dangerously wrong, and the analogy with comments is misleading. 
Everyone knows that comments are ignored by the interpreter, and even 
then, the ideal is to write self-documenting code, not comments:

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. "
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22

This part of your proposal would be *worse*: you would fool the casual 
or naive user into believing that Python did dimensional analysis, while 
in fact not doing so. You would give them a false sense of security.

Don't think of people writing code like this:

    result = 23mA + 75MHz

which is obviously wrong. Think about them writing code like this:

    total = sum_resistors_in_parallel(input, extra)

where the arguments may themselves have been passed to the current 
function as parameters from somewhere else. Or they may be data values 
read from a file. Their definitions may be buried deep in another part 
of the program. Their units aren't obvious to the reader without serious 
work.

This part of your proposal makes the language *worse*: we lose the 
simple data validation that "23mA" is not a valid number, but without 
gaining the protection of dimensional analysis.

To give an analogy, you are suggesting that we stick a sticker on the 
dashboard of our car saying "Airbag" but without actually installing an 
airbag. And you've removed the seat belt. The driver has to read the 
manual to learn that the "Airbag" is just a label, not an actual 
functioning airbag.


> Some people have suggested that we take the next step and use the units for 
> dimensional analysis, but that is highly problematic because you cannot do 
> dimensional analysis unless everything is specified with the correct units, and 
> that can be a huge burden for the user.

What?

That's the *whole point* of dimensional analysis: to ensure that the 
user is not adding a length to a weight and then treating the result as 
a time. To say that "it is too hard to specify the correct units, so we 
should just ignore the units" boggles my mind.

Any reasonable dimensional program should perform automatic unit 
conversions: you can add inches to metres, but not inches to pounds. 
There are already many of these available for Python.


-- 
Steve


More information about the Python-ideas mailing list