Aspect oriented programming (AOP) in Python
Asked Answered
L

2

37

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.

Lemniscus answered 10/9, 2012 at 17:33 Comment(6)
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?Salientian
@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 .Lemniscus
@esaelPsnoroMoN I was not asking about library support. But, if there is, or planned to be a language extension that supports AOP.Lemniscus
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.Nerissanerita
@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.Pneumatics
@Pneumatics I have read your answer. I come from a C family of languages, so for dynamic languages like Python I still do not comprehend all their features.Lemniscus
P
43

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://github.com/jsbueno/metapython/blob/main/aspect.py

Pneumatics answered 10/9, 2012 at 17:41 Comment(13)
hi @Pneumatics ... that is a very useful code snippet in bitbucket. Could you declare a software license on it? MIT or BSD would be awesome.Sixtyfour
@gnychis: done. I usually like my stuff "share alike", so I attached LGPL_v3 to the repository as a whole. Since you manifested interest in the AOP example alone, I left that one under MIT, so you don't feel constrained by anything. Just ask if you want anything else. (I'd probably like to pair up to upgrade that to a full Python module if you have a project to make use of it)Pneumatics
rock on, thanks so much @jsbueno! I definitely have a project that will be using it. I've found it to be a nice and simple implementation, whereas many other AOP libraries are overly complex.Sixtyfour
@Pneumatics Youth might want to translate the comments in the linked file to English so that more people can benefit from them.Aideaidedecamp
Super simple and readable. You can speed it up quite a bit by storing the list of matching pre/post functions that don't require args to match as precomputed lists. Built-in language support would speed it up a lot more.Ahlers
Even though Python is dynamic, ie you can change its structure and some of its behavior at runtime, AOP is still useful to provide clean crosscutting concerns management. If you decorate your code with external libraries (like for timings and logging) you still create a coupling between your business class and your libraries. In that regard, python is no exception to the rule. One could argue that removing and adding a decorator is easy, I agree, but the point is the decouple any crosscutting from your business code. Finally, some decorators may prevent you from properly testing your code.Coney
Thank you for coming back to this aging answer - and, although I had not updated the code above, and not checked on the libraries, you will see that the possibility of Python dynamism go far beyond decorators - and it is perfectly feasible to have all desired aspects of AOP with simple libraries - as they can be made to intercept calls at run time. Using the same mechanisms for testing is also trivial. 2020 and "full AOP" is still a solution searching for a problem, and various partial solutions - including decorators and event systems can address the major pain points AOP should solve.Pneumatics
@Pneumatics I know you can intercept call at runtime and without using decorator explicitly, and my bad, I read the question in the wrong way -_-..... I just realized you wrote "Python doesn't need language extension to achieve AOP" which is totally true. I thought you wrote "Python doesn't need AOP", which is what I was arguing (the purpose of handling crosscutting concerns). I'll put back an upvote on your answer.Coney
OTOH, I recalled that there is actual a language extension to Python in that works like AspectJ for Java, but for adding functional language characteristics. It is "coconut" - coconut-lang.org ; so yes, having enough well documented features to add, this is a feasible thing that can bring improvements - however, probably a whole new superset of the language would have less traction than a good library.Pneumatics
The code bucket link doesn't work as of 2021-02-19. If someone could add example code for this in the answer itself it would be helpfulPalladium
es - atlassian shut down mercurial repositories there in June/2020 - Thank ou for reminding me - I should re-upload this project.Pneumatics
The Python features that do aspect-oriented programming in Python are method decorators, class decorators, and metaclasses as these are run at before all other code, and they can use dynamic introspection/reflection capabilities of Python to implement advanced stuff.Menu
@Pneumatics this is brilliantKra
M
5

You can use Spring Python

Link : http://docs.spring.io/spring-python/1.2.x/sphinx/html/aop.html#aspect-oriented-programming

Montevideo answered 10/9, 2012 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.