Any AOP support library for Python?

In Python, aspect-oriented programming typically consists of dynamically modifying classes and instances at runtime, which is commonly referred to as monkeypatching. In an answer to another AOP question, I summarized some of these use cases for AOP in Python.


See S.Lott's link about Python decorators for some great examples, and see the defining PEP for decorators.

Python had AOP since the beginning, it just didn't have an impressive name. In Python 2.4 the decorator syntax was added, which makes applying decorators very nice syntactically.

Maybe if you want to apply decorators based on rules you would need a library, but if you're willing to mark the relevant functions/methods when you declare them you probably don't.

Here's an example for a simple caching decorator (I wrote it for this question):

import pickle, functools
def cache(f):
  _cache = {}
  def wrapper(*args, **kwargs):
    key = pickle.dumps((args, kwargs))
    if key not in _cache:
      _cache[key] = f(*args, **kwargs) # call the wrapped function, save in cache
    return _cache[key] # read value from cache
  functools.update_wrapper(wrapper, f) # update wrapper's metadata
  return wrapper

import time
@cache
def foo(n):
  time.sleep(2)
  return n*2

foo(10) # first call with parameter 10, sleeps
foo(10) # returns immediately

Edit: I no longer maintain pytilities and it has been unmaintained for years. You may want to consider one of the other answers instead or this list on Wikipedia.

Another AOP library for python would be pytilities (Documentation; svn repo). It is currently the most powerful (as far as I know).

Its features are:

  • make reusable Aspect classes
  • apply multiple aspects to an instance or class
  • unapply aspects to an instance/class
  • add new attributes to an instance by using an aspect
  • apply advice to all attributes of an instance/class
  • ...

It also has other goodies such as some special descriptors (see the documentation)

Tags:

Python

Aop