[Python-ideas] Units in type hints

Koos Zevenhoven koos.zevenhoven at aalto.fi
Fri May 15 16:21:58 CEST 2015


Thanks for the email and tip!

For my own code, I tend to always use SI units or those derived from 
them. If I want 3 milliseconds, I do 3e-3. Although seconds are pretty 
universal, not everyone has the privilege of being born and raised in SI 
units :P. Well, I guess m/s is rarely the everyday unit for speed, anywhere.

For me, the problems arise when there are third-party non-SI functions 
or things like functions that take a duration in terms of samples of 
discretized signals (potentially Int['samples'] or in some cases 
Float['samples']).

-- Koos


On 2015-05-14 17:05, Skip Montanaro wrote:
> On Thu, May 14, 2015 at 6:03 AM, Koos Zevenhoven
> <koos.zevenhoven at aalto.fi> wrote:
>> How about extending the type annotations for int, float and complex to
>> optionally include also a unit?
> Not sure that's going to fly, but you might want to check out the
> magnitude package:
>
> https://pypi.python.org/pypi/magnitude/0.9.1
>
> I've used it in situations where I want to specify units scaled to a
> more natural (to me) size. For example, the gobject.timeout_add
> function takes a delay in milliseconds. Given that most of the time I
> want delays in seconds or minutes, it's much more natural for me to
> let magnitude do the work silently.
>
> Skip



More information about the Python-ideas mailing list