I’d like to automatically run some code upon class creation that can call other class methods. I have not found a way of doing so from within the class declaration itself and end up creating a @classmethod called __clsinit__ and call it from the defining scope immediately after the class declaration. Is there a method I can define such that it will get automatically called after the class object is created?
I’d like to automatically run some code upon class creation that can call other
Share
You can do this with a metaclass or a class decorator.
A class decorator (since 2.6) is probably easier to understand:
Metaclasses are more powerful; they can call code and modify the ingredients of the class before it is created as well as afterwards (also, they can be inherited):