Reputation: 20880
I have a function remove_it()
that tries to remove some data and the removed data is added to the set removed
. The main logic is that if there is more to remove, keep removing and my code looks like this:
removed = set()
prev_count = -1
while prev_count != len(removed):
prev_count = len(removed)
remove_it()
It bothers me a little that the while
loop condition and the next line look very similar to each other. Is it normal or is there a better way to do it?
The logic in remove_it()
is quite complicated: it detects some graph structure topology and after each round of removal the topology changes and I cannot know how it changes until the removal is done.
I was thinking of return bool value from remove_it()
to track whether the set removed
has changed. Then the while loop would be like
while remove_it():
pass
which is also strange to me. Is there a better way?
Upvotes: 2
Views: 90
Reputation: 5304
Your remove_it
function has side effects and this makes program harder to read. You can rewrite it so that instead of modifying global removed
variable it would return set of removed values. Then you can rewrite the loop:
removed = set()
while True:
removed_batch = remove_it()
if removed_batch:
removed += removed_batch
else:
break
Upvotes: 2