How about this:
>>> def foo(start=0, end=10, num_of_elements=4): ... if (end-start)/num_of_elements != float(end-start)/num_of_elements: ... end = float(end) ... while start < end: ... yield start ... start += end/num_of_elements ... >>> list(foo(0, 10, 4)) [0, 2.5, 5.0, 7.5] >>> list(foo(0, 10, 3)) [0, 3.3333333333333335, 6.666666666666667] >>> list(foo(0, 20, 10)) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>>
It saves your specification so that the first element is integer and the rest are floating. But if you give him a step where the elements can remain intact, they will be.
source share