Use method decorators to add some marker attributes to interesting methods and use a metaclass that iterates over methods, finds marker attributes, and executes logic. The metaclass code runs when the class is created, so it has a link to the newly created class.
class MyMeta(object): def __new__(...): ... cls = ... ... iterate over dir(cls), find methods having .is_decorated, act on them return cls def decorator(f): f.is_decorated = True return f class MyBase(object): __metaclass__ = MyMeta class MyClass(MyBase): @decorator def bar(self, foo): print foo
If you are worried that the MyClass programmer forgets to use MyBase , you can force the metaclass to be set in decorator by examining the global list of frames of the calling stack ( sys._getframe() ).
source share