Home

Awesome

method_decorator

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:

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, method_decorator:

Usage:

pip install method_decorator
from method_decorator import method_decorator

class my_decorator(method_decorator):
    # ...

See full unit-tests for usage details.

method_decorator version 0.1.3
Copyright (C) 2013 by Denis Ryzhkov denisr@denisr.com
MIT License, see http://opensource.org/licenses/MIT