Looping in Python
basegmez
fb at ultranet.com
Mon Dec 17 12:02:42 EST 2001
If everything is an object in Python then why not:
Syntax:
loop(start=0, stop, step=1):
(loop.count and loop.value automagically generated by Python,
loop count starts from 1 and incremented by 1,
loop value starts from start value and incremented by step value,
stop value is a long integer)
if condition:
break
elif anothercondition:
continue
your_statements_and_what_have_you_here
Example 1:
Input:
loop(-4,4,2):
print loop.count, loop.value
Output:
1 -4
2 -2
3 0
4 2
Example 2:
Input:
loop(4):
print loop.count, loop.value
Output:
1 0
2 1
3 2
4 3
If you feel like pushing it, "loop.start", "loop.stop" and "loop.step" may
be added.
If it could implement floating point step values, it would be even better
but I am not sure if this would be feasible.
I realized that there have been some lengthy (to put it mildly) discussions
of loop structures, if this is already proposed and rejected, just ignore
it. If not, someone may be able to come up with better alternatives to
"loop.count" and "loop.value" even the "loop" keyword. This was the most
Pythonic way I could come up with.
Regards,
Fahri
More information about the Python-list
mailing list