ABC with abstractmethod: kwargs on Base, explicit names on implementation
Samuel Marks
samuelmarks at gmail.com
Sat Aug 29 05:14:54 EDT 2020
So there is no way to get a AOT error when the two functions aren't
implemented, if the two functions have different required arguments on
implementors?
To paint this picture for why I need this, say the first is:
class Base(ABC):
@abstractclass
def train(self, epochs):
asset epochs is not None and epochs > 0
…and the implementation is:
class TensorFlow(Base):
def train(self, learning_rate, epochs, callbacks, metrics, loss, optimizer):
super(Base, self).__init__(epochs=epochs)
[+ docstrings and PEP484 types; excluded here for concision]
So how do I enable this use-case? - Obviously it doesn't make sense to
include these on the base class, and giving them default values
probably doesn't make sense either.
You're saying I shouldn't be using ABC for this. So what should I be using?
The requirement I'm trying to enforce is that each implementor has a
`train` callable attached.
Preferably with one required field (but this is just a nice-to-have).
BTW: I have enabled an argument inheritance use-case through creating
a library with the builtin `ast` module. So now you can have `def
train_c(self, config)` with config referring to an instance of a class
which inherits from a base config class. See here for the
implementation https://github.com/SamuelMarks/doctrans
However if I only have functions which accept an instance of a class
as the argument, then that will make it less user-friendly to the API
caller. So I really am looking for handling both interfaces in a
straightforward manner.
Thanks for your suggestions,
Samuel Marks
Charity <https://sydneyscientific.org> | consultancy
<https://offscale.io> | open-source <https://github.com/offscale> |
LinkedIn <https://linkedin.com/in/samuelmarks>
On Fri, Aug 28, 2020 at 3:51 AM Dieter Maurer <dieter at handshake.de> wrote:
>
> Samuel Marks wrote at 2020-8-27 15:58 +1000:
> >The main thing I want is type safety. I want Python to complain if the
> >callee uses the wrong argument types, and to provide suggestions on
> >what's needed and info about it.
> >
> >Without a base class I can just have docstrings and type annotations
> >to achieve that.
> >
> >What can I use that will require all implementers to have a minimum of
> >the same properties and arguments, but also allow them to add new
> >properties and arguments?
>
> A main paradigm of object oriented programming is the
> ability to use a derived class instance with knowledge only
> about the base class. This implies that in many cases, you
> need not know the concrete class because any instance of a derived
> class must have the essential behavior of the base class instances.
>
> This paradigm imposes limitations on the allowable signature changes.
> An overriding method may add further parameters but all those
> must have default values - otherwise, the use with base class knowledge
> only would cause errors.
>
> > Preferably I would like this all to happen before compile/interpret
> time.
>
> Use a "lint" version to check compatibilty.
More information about the Python-list
mailing list