[Python-ideas] SI scale factors in Python

Steven D'Aprano steve at pearwood.info
Fri Aug 26 07:29:18 EDT 2016


On Fri, Aug 26, 2016 at 06:31:30PM +1000, Nick Coghlan wrote:
> On 26 August 2016 at 16:54, Steven D'Aprano <steve at pearwood.info> wrote:
> > At best, we can choose descriptive variable names that hint what the
> > correct dimensions should be:
> >
> >     weight_of_contents + weight_of_container
> >
> >
> > The argument that units would make it easier for the programmer to spot
> > errors is, I think, invalid, because the programmer will hardly ever get
> > to see the units.
> 
> This is based on a narrowly construed definition of "programming"
> though. It makes more sense in the context of interactive data
> analysis and similar activities, where Python is being used as a
> scripting language, rather than as a full-fledged applications
> programming language.

I don't think that is right. I'd put it the other way: as important as 
interactive use is, it is a subset of general programming, not a 
superset of it.

I *love* using Python as a calculator (which is why this thread is 
inspiring me to investigate the unit conversion/tracking packages 
already available). But even when using Python as a calculator, oh I'm 
sorry, "for interactive data analysis" *wink*, there are going to be 
plenty of opportunities for me to write:

    x = ...
    y = ...
    # much later on
    z = x + y

so that I don't necessarily see the units directly there on the screen 
by the time I actually go to use them. Likewise if I'm reading my values 
from a data file. IPython even generalises the magic variable _ into a 
potentially unlimited series of magic variables _1 _2 _3 etc, and it is 
normal to be using values taken from a variable rather than as a 
literal.

The point is that Ken's examples of calculations on literals is 
misleading, because only a fraction of calculations involve literals. 
And likely a small fraction, if you consider all the people using Python 
for scripting, server-side programming, application programming, etc
rather than just the subset of them using it for interactive use.

By the way, here's another programming language designed for interactive 
use as a super-calculator. Frink does dimensional analysis and unit 
conversions:

http://frinklang.org/#SampleCalculations

If we're serious about introducing dimension and unit handling to 
Python, we should look at:

- existing Python libraries;
- HP calculators;
- Frink;

at the very least.


-- 
Steve


More information about the Python-ideas mailing list