I needed something like that and while I think that callback should always be a function, AFAIK there's no way to specify the type of a function and I kept reusing callbacks with the same arguments doing different things so that's what I came up with.
import abc
class BaseCallback(metaclass=abc.ABCMeta):
def __init__(self, param: int):
self.param = param
self.do_things()
@abc.abstractmethod
def do_things(self) -> int:
raise NotImplementedError
class MyCallback(BaseCallback):
def do_things(self):
return self.param * 2
def func(callback: BaseCallback): # type of callback specified
callback()
func(callback=MyCallback)
You don't need to run the do_things
method in init (imo it's ugly), depends on how much power do you have regarding where/when callback is run.