Created
April 1, 2017 22:04
-
-
Save void4/3004e9e08f88dd7b2f060db081b09396 to your computer and use it in GitHub Desktop.
Python, Time, Interval, __bool__, __nonzero__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Object which is true once after <interval> seconds | |
# For Python2, use the __nonzero__ method | |
class Every: | |
def __init__(self, interval): | |
self.interval = interval | |
self.lasttime = time() | |
def __bool__(self): | |
current = time() | |
if current-self.lasttime>=self.interval: | |
self.lasttime = current | |
return True | |
return False | |
every = Every(1) | |
while True: | |
if every: | |
print("One second passed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment