I need a simple class or function that takes a test (callable object which returns True or False) and a function to be called when the test is True, potentially doing the whole thing in a different thread maybe. something like this:
nums = []
t = TestClass(test=(lambda: len(nums) > 5),
func=(lambda: sys.stdout.write('condition met'))
for n in range(10):
nums.append(n)
time.sleep(1)
#after 6 loops, the message gets printed on screen.
Any help is appreciated. (Please, nothing too complicated since I am still a beginner)
You’re correct in thinking you might need a separate thread to check a condition in the background. In this separate thread, you also have to decide how often you want to check (there would be other ways to do this, but this way requires the least changes to the code you’ve shown).
My answer just uses a function, but you could easily use a class instead if you like: