Currently, I'm doing it in this fashion:
class Spam(object):
decorated = None
@classmethod
def decorate(cls, funct):
if cls.decorated is None:
cls.decorated = []
cls.decorated.append(funct)
return funct
class Eggs(Spam):
pass
@Eggs.decorate
def foo():
print "spam and eggs"
print Eggs.decorated # [<function foo at 0x...>]
print Spam.decorated # None
I need to be able to do this in a subclass as shown. The problem is that I can't seem to figure out how to make the decorated
field not shared between instances. Right now I have a hackish solution by initially setting it to None
and then checking it when the function is decorated, but that only works one way. In other words, if I subclass Eggs
and then decorate something with the Eggs.decorate
function, it affects all subclasses.
I guess my question is: is it possible to have mutable class fields that don't get shared between base and sub classes?