29
votes

Possible Duplicate:
Any AOP support library for Python?

I am familiar with the AspectJ extension for the Java language.

I want to know if there is such a thing for Python.

Don't get me wrong, I do not mean a library but a language extension like AspectJ is to Java.

2
Keep in mind that Python != Java. So something that requires a full "language extension" in Java might be performed by an entirely different mechanism in Python (including, perhaps, just a library). Are there any particular AOP aspects being looked for?user166390
@pst AspectJ is kind of de facto for Java. As I know, there are some libraries for Python that implement AOP, but I am not sure of their mainstream acceptance .coredump
@esaelPsnoroMoN I was not asking about library support. But, if there is, or planned to be a language extension that supports AOP.coredump
I've never really seen a convincing example of AOP. Not saying there aren't uses, but the examples always look like a way to make code much more difficult to understand for no benefit.Marcin
@coredump: As I stated more at length on my answer: there is no way that could be a "planned language extension that supports AOP", because teh langauge already does so, from scratch, due to its dynamic nature.jsbueno

2 Answers

37
votes

Python does not need something like a "language extension" for being able to work in an Aspect Oriented way.

That is simply due to the dynamic mechanisms in Python itself. A Google search will yield a couple projects - but despite looking merely like libraries, it is all that is needed in Python.

I am not making this up - it is the fact that you can introspect classes and methods, and change them at run-time. When I first learned about Aspect Orientation, I could implement some proof of concepts in Python in a couple of hours - certainly some of the existing projects can offer production-quality entries.

But since you asked, there is a Python "language extension" of sorts that could be used for Aspect Orientation: when I made the proof of concept I mentioned above, I used to check the input parameters to methods at run-time to determine whether certain methods would be affected by a rule or not.

In Python 3 there is a little known feature of the language that allows one to annotate the input parameters and return value of a function or method. An aspect orientation library could make use of this to apply its magic at "load time", and not at the time of each function call.

BTW, here is my quick hack to get a working example of using Aspect Orientation with Pure Python. Sorry - the code comments are in pt_BR - https://bitbucket.org/jsbueno/metapython/src/f48d6bd388fd/aspect.py