[Python-ideas] Units in type hints

Gregory Salvan apieum at gmail.com
Fri May 15 18:13:26 CEST 2015


Hi,
why don't you try a tuple of types ?
(Float, Samples)

Period:
(int, Time)
and if you want to force seconds eventually:
(int, Time[seconds])


2015-05-15 16:21 GMT+02:00 Koos Zevenhoven <koos.zevenhoven at aalto.fi>:

> 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
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150515/311cebba/attachment.html>


More information about the Python-ideas mailing list