The documentation basically says that range
must behave exactly as this implementation (for positive step
):
def range(start, stop, step):
x = start
while True:
if x >= stop: return
yield x
x += step
It also says that its arguments must be integers. Why is that? Isn't that definition also perfectly valid if step is a float?
In my case, I am esp. needing a range
function which accepts a float type as its step
argument. Is there any in Python or do I need to implement my own?
More specific: How would I translate this C code directly to Python in a nice way (i.e. not just doing it via a while
-loop manually):
for(float x = 0; x < 10; x += 0.5f) { /* ... */ }
return
andyield
keywords in that loop, usebreak
. – Tim McNamara Nov 15 '10 at 23:12float_range
- done. – Mark Ransom Nov 15 '10 at 23:27return
becomesraise StopIteration
;return <expression>
getsSyntaxError: 'return' with argument inside generator
-- even forreturn None
. – John Machin Nov 15 '10 at 23:28range()
returns a list. What the OP describes is actuallyxrange()
, which returns an element at a time. – BobC Mar 15 '13 at 15:49