0

Let's say I define a base class that provides some methods that will likely be overridden. It's not required though, as the base class provides naive default implementations. How can I highlight those methods best?

In C++, I would just use virtual but Python is a dynamic language where methods can always be overridden without marking them. I am rather looking for a hint here. Ideally, this is faster to see than a textual explanation in the docstring and easier to understand to others than a custom decorator.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
danijar
  • 32,406
  • 45
  • 166
  • 297
  • expressive docstrings? a decorator? – obataku Aug 12 '16 at 04:15
  • 2
    Possible duplicate of [Python - how to implement virtual methods?](http://stackoverflow.com/questions/4714136/python-how-to-implement-virtual-methods) – Dave Aug 12 '16 at 04:16
  • @dave It's not a duplicate. Python methods are always virtual but I'm looking for a way to *suggest* to override methods to developers inheriting from my class. – danijar Aug 12 '16 at 04:22
  • @oldrinb Thanks. Both are valid options, but have their downsides as I pointed out in the updated question. Is there a better solution? – danijar Aug 12 '16 at 04:23
  • A decorator that issues a warning upon (first) method invocation? Something like [@deprecated](http://code.activestate.com/recipes/391367-deprecated/) – user3159253 Aug 12 '16 at 05:10
  • I think an example or two would make your question more clear... or at least, your intent. When you "suggest" a method for overriding, are you trying to say something like "subclasses may not override anything _but_ this", or something less strict, like "subclasses will find overriding _this_ more useful than overriding other methods"? (On re-reading, I see you're not interested in the third option, "subclasses _must_ override this", but I'll leave it here for completeness.) – Kevin J. Chase Aug 12 '16 at 05:11
  • @KevinJ.Chase The last case, "subclasses might find it useful to override this", is exactly what I mean. An alternative would be to prevent all other methods from being overridden, but that possibility is not provided in Python. – danijar Aug 12 '16 at 05:13
  • 1
    From what (little) I've seen, methods like this are highlighted and explained in the documentation, not with any language feature. – Kevin J. Chase Aug 12 '16 at 05:28

0 Answers0