This Python decorator knows the class the decorated method is bound to.
It is unusual for Python, because, when the decorator is applied to just-defined method, the class does not exist yet.
And once this class is not known in advance, it is almost impossible to get this knowledge, while desired.
I needed this when I was implementing RPC of class-methods (and other types of functions) over AMQP.
It is also needed for some other people, according to this Stack Overflow question.
From my answer there:
Ideas proposed here are excellent, but have some disadvantages:
args.__class__.__name__are not suitable for plain functions and static-methods.
__get__must be in a class, that is rejected by
@wrapsitself should be hiding traces better.
So, I've combined some ideas from that page, links, docs and my own head,
and finally found a solution, that lacks all three disadvantages above.
As a result,
- Knows the class the decorated method is bound to.
- Hides decorator traces by answering to system attributes more correctly than
- Is covered with unit-tests for bound an unbound instance-methods, class-methods, static-methods, and plain functions.
pip install method_decorator from method_decorator import method_decorator class my_decorator(method_decorator): # ...