Py­thon is, as in­dic­ated by of­fi­cial py­thon tu­torial, the first doc­u­ment most Py­thon new­bies en­coun­ter, a dy­nam­ic­ally typed lan­guage. While de­vel­op­ing I learned to not worry about small de­tails and let duck typ­ing to do it’s thing.

The prob­lem

Un­der­stand­ing the pur­pose of range is a simple mat­ter. You give it point at which you want your arith­metic pro­gres­sion ter­min­ate and op­tion­ally what num­ber your se­quence should be­gin with and amount by which every con­sec­ut­ive num­ber in the se­quence should in­crease.

All is fine and good un­til you de­cide you want your se­quence to in­crease in steps of 110\frac{1}{10}. I shall try it out by simply passing a 0.1 as last ar­gu­ment.

Strangely enough this be­ha­viour is in­ten­tional and there are no traces of code to sup­port any kind of dy­nam­ism for this func­tion. The reason of course is per­form­ance. Oth­er­wise nobody would use them in their highly op­tim­ised, tight loops, right? On the other hand in­ac­cur­acy of some data types as poin­ted out by Stack­Over­flow com­munity might be yet an­other reas­on.


Now, in case you do not have any prob­lems with pulling in whole numpy lib­rary as a de­pend­ency you can use numpy.arange. Oth­er­wise you might sim­u­late it with list com­pre­hen­sions or map:

This solu­tion is func­tional and terse, however, it is neither con­veni­ent nor beau­ti­ful. Py­thon could do bet­ter job here! A wrap­per func­tion us­ing reg­u­lar range given in­tegers and fall­ing back to slower code for everything else sounds reas­on­able. At least people would not need to map over val­ues to get them in form they need.