Reputation: 305
I would like to understand how to pass an external function to a class method. For example say I am trying to call a function 'job' every second.
import schedule
import time
def set_timer_minutes(func):
schedule.every(1/60).minutes.do(func)
def job():
print("I'm working...")
set_timer_minutes(job)
while 1:
schedule.run_pending()
time.sleep(1)
The above code prints 'I'm working' every second. But if I try to put it in a class
class Scheduler:
def set_timer_minutes(self,func):
schedule.every(1/60).minutes.do(func)
while 1:
schedule.run_pending()
time.sleep(1)
def job():
print("I'm working...")
x= Scheduler
x.set_timer_minutes(job)
I get
TypeError: set_timer_minutes() missing 1 required positional argument: 'func'
Upvotes: 1
Views: 223
Reputation: 81976
You need to create an instance of Scheduler.
x = Scheduler()
instead of
x = Scheduler
Upvotes: 3