I have a small class with a timer, but don’t have anything for the timer to do when it expires. Since Timer extends Thread, I get all I want to know from calling timer.isAlive(). Since Timer requires a function parameter, I’m looking for an anonymous ‘nop’ function that doesn’t clutter the class, and came up with this;
def ready(self, node):
def nop(): pass
delay = node.find('delay')
seconds = float(delay.get('seconds'))
self.timer = Timer(seconds, nop)
return True;
It seems to work with my little test programs. Having not tried this before, or seen this as an example anywhere, I’m wondering, is this truly safe?
Your code is fine. Functions doing nothing aren’t dangerous — they just do nothing. Using a lambda function would make the code a bit more concise: