I am making an abstract base class, the public methods for which require child classes to implement the abstract methods with certain parameters. How can I write the abstract method function definition, beyond just writing a comment, to indicate that child classes should have certain parameters in the abstract method itself?
My reason for writing such an abstract method is because the public method calls scipy.optimize.curve_fit, which takes a callable as an argument and that callable itself must have certain parameters in its definition.
Here is some pseudocode for clarification:
from abc import ABC, abstractmethod
from scipy.optimize import curve_fit
class DiseaseModel(ABC):
def fit(self, t, ydata):
return curve_fit(self._fit, t, ydata)
@abstractmethod
def _fit(self, t, modelParam1, modelParam2, ..., modelParamN):
"""Method fits parameters of a model to data.
This method MUST have `t` (which is just timesteps over which a
certain ydata occurred) AND any other parameters relevant for the
model of the system. Should I maybe just use `*args` or `**kwargs`
in the function definition?
For example, if a child class is for a simple
SIR epidemic model, then the function definition should be
`def _fit(self, t, beta, gamma)`.
Likewise, for a child class
defining a demographic SIR model, the function definition should be
`def _fit(self, t, beta, gamma, mu)`.
"""
pass
Well, your question is not really clear.
Abstract methods are meant to be overridden by the developers to their own needs. depending on what you need to communicate to the future developers, I would recommend few things that could potentially help you. Keep in mind, that you can do only so much and some point you should consider that developers can also think by themselves and figure out. Nevertheless, these are my suggestions:
Add typing for both expected arguments and function return type. If you do so ,for the other non-abstract functions that depend on your fit
function, they will raise typing error and to certain extend enforce a specific implementation for the fit function.
Please take a look at This Solution! to illegal arguments. You can create explicit value errors that you can raise within other non-abstracted functions of your class to enforce a standard if you need to.
Keep docstring for clear definition of your expectations. Granted that people are using IDEs, they will be notified of these expectations.
Update and mention this in your documentations , i.e. Readme, confluence wiki, etc.
Again, there is only so much you can do to communicate this ;)