0

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?

1
  • 2
    You really don't need that semicolon at the end. Commented Mar 29, 2012 at 21:55

1 Answer 1

1

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:

self.timer = Timer(seconds, lambda: None)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.