Accessing a decorator in a parent class from the child in Python?

Class baseclass(object): def __init__(self): print 'hey this is the base' def _deco(func): def wrapper(*arg): res = func(*arg) print 'I\'m a decorator. This is fabulous, but that colour, so last season sweetiedarling' return res return wrapper @_deco def basefunc(self): print 'I\'m a base function' @_deco def basefunc2(self): print "I'm another base function" #no more uses of _deco in this class _deco = staticmethod(_deco) # this is the key. It must be executed after all of the uses of _deco in # the base class.

This way _deco is some sort weird internal function that # can be called from within the class namespace while said namespace is being # created and a proper static method for subclasses or external callers. Class otherclass(baseclass): def __init__(self): super(otherclass, self). __init__() print 'other class' @baseclass.

_deco def meh(self): print 'I\'m a function.

1; much better than my answer (now deleted). – Adam Bernier Aug 6 '10 at 15:22 Fabulous, I'll have some of that, that is just so in this season, sweetiedarling. (I thought I'd just go with the theme.) – dochead Aug 8 '10 at 13:13.

You can't because at the point of decoration, no class owns the method f. Clearly, the returns() decorator is built before the function is assigned to an owner class. Now when you write a function to a class (either inline, or explicitly like this) it becomes an unbound method object.

So you can read f. Im_class inside ‘new_f’, which is executed after the assignment, but not in the decorator itself. (And even then it's a bit ugly relying on a CPython implementation detail if you don't need to.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions