0

Some context:

  • I'm creating a very simple component-entity 2d game engine

  • Vast majority of classes will have flat subclass hierarchies

  • Pretty much all base classes don't require any arguments

  • I'm using Python 2

So, the current solution, is to make __init__ of base classes call overridable _on_init method, because having to do BaseClass.__init__ strikes me as unsightly, and so does having to use super()

I'm not necessarily asking about your thoughts on this practice(although they are by all means welcome), but mainly just asking if there aren't any pitfalls or other unpleasantries that I might be subscribing myself to.

Llamageddon
  • 3,306
  • 4
  • 25
  • 44
  • 2
    Here's one reason why you wouldn't want to do this: readability. – Joel Cornett Feb 18 '13 at 18:05
  • How would it make it less readable? There is no multiple inheritance involved, and it's obvious what class you're inheriting from. No call to __init__ might not be what most Python users are used to, but it's a very small, pretty much cosmetic difference. – Llamageddon Feb 18 '13 at 18:19
  • 1
    Exactly because "No call to `__init__` might not be what most Python users are used to." If you feel that this is worth sacrificing, then make sure that you document what you're doing explicitly and clearly. – Joel Cornett Feb 18 '13 at 19:20
  • 1
    There's also the fact that "Vast majority of classes…" is not the same as "All classes". What should grandchild classes do in this case? Call super's `_on_init` at the start? Or the end? Or the static base class's? When you're using `__init__`, there are well-known idioms that answer those questions; when you're inventing a new scheme, there are not. And it gets even worse if multiple inheritance (even diamondless MI with mixin classes) is conceptually reasonable. – abarnert Feb 18 '13 at 19:27
  • 1
    Also, what do you need a metaclass for here? Even if you want to do this, why not have `Base.__init__(self, *args)` just call `self._on_init(*args)`? That would have the same effect, and it would be much easier for developers of derived classes to understand. – abarnert Feb 18 '13 at 19:30

0 Answers0