I'm developing a class in Python, in which one of its attributes is supposed to represent a neural network. I would like this attribute to actually be an instance of another class, let's call it NN
, with several methods (like train()
for training, predict()
for prediction, etc.). Furthermore, I thought a reasonable way to initialize this attribute was by using my class' constructor, which would expect an instance of the NN
class as one of its arguments.
My first thought on how to solve this was to implement an interface, and then type check whatever it is that the user supplies to my class constructor, in order to make sure it is from the expected NN
class. However, it seems like neither interfaces (1) nor typechecking (2, 3, 4, 5) are good practices in Python.
Instead, we're supposed to use Duck typing, and simply try using the objects methods until we can't (i.e. defer type checking to runtime). However, in my case the NN
class might be quite complicated, with several methods and attributes. If I do discard the interface idea, and simply expect the user of my class to provide an object with all the necessary methods and attributes, how will he/she know what is expected of this object? Should I write all this information in my class docstring, in my constructor docstring, or somewhere else? Or is there a better way to solve this scenario?