Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspect oriented programming (AOP) in Python

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.

like image 550
coredump Avatar asked Sep 10 '12 17:09

coredump


People also ask

What is AOP in Python?

Aspect oriented programming (AOP) is a horizontal programming paradigm, where some type of behavior is applied to several classes that don't share the same vertical, object-oriented inheritance.

What is meant by Aspect-Oriented Programming?

Aspect-oriented programming (AOP) is an approach to programming that allows global properties of a program to determine how it is compiled into an executable program. AOP can be used with object-oriented programming ( OOP ). An aspect is a subprogram that is associated with a specific property of a program.

What is Aspect-Oriented Programming AOP )? How Spring uses AOP explain with examples?

AOP is like triggers in programming languages such as Perl, . NET, Java, and others. Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.

What is the reason to use Aspect-Oriented Programming?

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.


2 Answers

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

like image 191
jsbueno Avatar answered Sep 21 '22 00:09

jsbueno


You can use Spring Python

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

like image 20
Aghilas Yakoub Avatar answered Sep 23 '22 00:09

Aghilas Yakoub