I author a package (openpnm) that provides some generic classes, which we expect users to subclass, and I’ve recently come to wish that our generic classes could run some code AFTER the subclass has been initialized.
At the moment, the user writes their code (e.g. defining some constants, etc) in the
__init__ of their subclass, and the only ‘trick’ they need to remember is to call
super().__init__() at the top of the method.
But I would really like to be able to ‘clean’ up the initialization by running some code post-initialization. My exact use case is pretty convoluted, so I won’t dive into the details…but it did get me wondering if this has ever been considered by anyone else. I found some stackoverflow Q&As on this, but the solutions all suggest defining a metaclass, which seems to adds extra onus on the user (in addition to calling
super.__init__()), so I did’t love that approach.
It seems to me that something like a
__post__ method could be implemented and called automatically by python, and I could put my ‘clean-up/housekeeping’ code in there, and the user would never know. I feel like this is analogous the
__new__ method, which gets called BEFORE