here I want to reach a class that owns the decorated method f
You cannot, because at the decoration point no class owns the f method.
class A(object): @returns(int) def compute(self, value): return value * 3
The same as saying:
class A(object): pass @returns(int) def compute(self, value): return value*3 A.compute= compute
Clearly, the returns() decorator is created before the function is assigned to the owner class.
Now, when you write a function for a class (either built-in, or explicitly like this), it becomes an unrelated method object. Now it has a link to its owner class, which you can get by saying:
>>> A.compute.im_class <class '__main__.A'>
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 relies a bit ugly on the details of the CPython implementation if you don't need it. I'm not quite sure what you are trying to do, but things related to "get the owner class" are often done using metaclasses.)
source share