Let’s say I have a class
class Foo:
def __init__(self):
pass
def meth1(self, *args):
do_stuff()
def meth2(self, **kwargs):
more_stuff()
def callme(self):
print "I'm called!"
and I want to smartly modify it so that callme() is called after executing meth1, meth2 and everything else. How do I do that? I’ve never used either class decorators or metaclasses, so I’m not sure which is the way to go and also how to use them properly.
With metaclasses, I’ve put together the following:
from types import FunctionType
def addcall_meta(name, bases, dct):
newdct = dict(dct)
for k, v in dct.items():
if isinstance(v, FunctionType) and k not in ('__metaclass__', 'callme'):
# the above can include more filters such as not startswith, etc.
print k, 'is a function!'
def newmethod(self, *args, **kwargs):
v(self, *args, **kwargs)
dct['callme'](self)
newdct[k] = newmethod
return type(name, bases, newdct)
With a class decorator:
def addcall_deco(cls):
dct = {}
for k, v in cls.__dict__.items():
if isinstance(v, FunctionType) and k != 'callme':
def newv(self, *args, **kwargs):
v(self, *args, **kwargs)
self.callme()
cls.k = newv
return cls
Both seem to work for my super-simple example:
In [75]: Foo()
I'm called!
Out[75]: <__main__.Foo instance at 0x21ed680>
Are there obvious reasons to use one and not the other (such as specific corner cases, etc.)? I also figure I could somehow use __new__, but from the common-sense point of view, I want to modify the class, not all of its instances.
Actually you are using your metaclass as a class decorator here – not really needing the more flexible things that are allowed with metaclasses.
Therefore, both implementations being the same, you should go witht he simpler approach – which is class decorators. As for “backwards compatibility” – it is not an issue you should worry about in a new project.
Of course, a lot of servers out there are running some very old Linuxes due to “long term support” editions, and feature Python versions as old as 2.4 or 2.5 – but if
your project is indeed being used in such an older server, the user can always install an updated enough to be supported Python as a normal user – no need to use the system Python.
On a side note: beware on your wrapper that you are discarding the wrapped method return value, if there is any. The way to do it is:
And finally, if you think you will need this kind of approach a lot on any project you are dealing with, take a look at “Aspect Oriented Programing” – There are a couple of projects out there that allow an aspected oriented approach in Python without many changes in your toolchain or way of working.