1

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    
Jared Frazier
  • 413
  • 1
  • 4
  • 10
  • What do you mean "tell the user"? A proper IDE will tell the programmer what to do when extending your class. – luk2302 Sep 21 '22 at 11:05
  • I mean the abstract method function definition should itself indicate that the programmer should implement the method in child classes with a parameter for `t` AND any other parameters that are relevant to the model to be fit – Jared Frazier Sep 21 '22 at 11:06

1 Answers1

2

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 ;)

Hirad Gorgoroth
  • 389
  • 2
  • 4
  • 13
  • Maybe the question is clearer now? I think if you look at the `_fit(...)` method comment, what i want might become more clear, but i'm not sure how to make the question more clear without this example – Jared Frazier Sep 21 '22 at 11:13
  • Your question is a bit more clear now :) I update my answer to point out a few approaches. If you wanna make sure you've done everything you could, go for all of them as they are best practice anyway. Good luck – Hirad Gorgoroth Sep 21 '22 at 11:34