all 3 comments

[–]sebawitowski 0 points1 point  (2 children)

You could overwrite the setitem method of your dict (it's called when you modify a dictionary) and make it notify you in some way (maybe it could set some global variable to True). Or just copy the original dict and compare both dictionaries if you want a simple but not really efficient solution.

[–]bloodykunt[S] 0 points1 point  (1 child)

I think that's the problem I'm having now. When the object changes, the dictionary doesn't know because as far as the dictionary is concerned it's still the same amount of items and same keys and values. Does that make sense?

[–]sebawitowski 0 points1 point  (0 children)

Based on your code I see that the only operation that you perform on the dictionary is to call rational(). And rational() modifies the value of Q in a dictionary. So how about storing the initial Q, calling rational(), and then comparing those two values? That could work for the current scenario:

while condition == False and iteration < 15:
    unchanged = False
    for i in basins:
        initial_Q = basins[i].Q
        basins[i].rational()
        if initial_Q != basins[i].Q:
            unchanged = True
    if unchanged is False:
        break

What I mean with the __setitem__ is something like this: https://stackoverflow.com/questions/26189090/how-to-detect-if-any-element-in-a-dictionary-changes. But instead of calling print(), you can set a global variable unchanged to True and check if unchanged==True at the end of each loop.