AOP use cases

Jacek Generowicz jacek.generowicz at cern.ch
Thu Apr 22 18:53:58 EDT 2004


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> Stupid example:

Let's give a better one, which is still short

# Mix concerns of calculating Fibonnaci numbers, and avoiding
# recalculating the same answer repeatedly

def fib(n):
    if n<2: return 1
    try:
        return fib.cache[n]
    except KeyError:
        return fib.cache.setdefault(n, fib(n-1)+fib(n-2))
fib.cache = {}

###############################################################

# Deal with avoiding recalculating the same answer repeatedly
def memoize(fn):
    cache = {}
    def proxy(*args):
        try:
            return cache[args]
        except KeyError:
            return cache.setdefault(args, fn(*args))
    return proxy

# Deal with calculating Fibonnaci numbers
def fib(n):
    if n<2: return 1
    return fib(n-1) + fib(n-2)

# Weave
fib = memoize(fib)



> Is this AOP? Should I care?
> 
> Is it useful? Should I care?
> 
> (I guess the way one answers questions 2 and 4, is ... err
> ... interesting.)



More information about the Python-list mailing list