R
RajNewbie
Hi,
My code has a lot of while loops of the following format:
while True:
...
if <condition>: break
The danger with such a code is that it might go to an infinite loop
- if the <condition> never occurs.
Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?
I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break
But I am not very happy with this code for 3 reasons
1. Verbosity (i=0 and i++) which doesnt add to the logic
2. The loop now has dual focus. - incrementing i, etc.
3. <most important> A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.
The solution that I had in mind is:
while True:
...
if <condition>: break
if inifinte_loop(): raise infiinte_loop_exception
Wherein infinite_loop is a generator, which returns true if i > 200
def infinite_loop():
i = 0
while i < 200:
i++
yield False
yield True
Could somebody let me know whether this is a good option?
One of my main worries is - what will happen if I call this same
procedure from another loop? Will it start again from 0 or will it
again start from my previous stored i? i.e.
def big_proc:
while True:
...
if infinite_loop: raise
while True:
...
if infinite_loop: raise
In such a case, will we run it 200 times both the times or not?
Could someone chip in with other suggestions?
My code has a lot of while loops of the following format:
while True:
...
if <condition>: break
The danger with such a code is that it might go to an infinite loop
- if the <condition> never occurs.
Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?
I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break
But I am not very happy with this code for 3 reasons
1. Verbosity (i=0 and i++) which doesnt add to the logic
2. The loop now has dual focus. - incrementing i, etc.
3. <most important> A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.
The solution that I had in mind is:
while True:
...
if <condition>: break
if inifinte_loop(): raise infiinte_loop_exception
Wherein infinite_loop is a generator, which returns true if i > 200
def infinite_loop():
i = 0
while i < 200:
i++
yield False
yield True
Could somebody let me know whether this is a good option?
One of my main worries is - what will happen if I call this same
procedure from another loop? Will it start again from 0 or will it
again start from my previous stored i? i.e.
def big_proc:
while True:
...
if infinite_loop: raise
while True:
...
if infinite_loop: raise
In such a case, will we run it 200 times both the times or not?
Could someone chip in with other suggestions?