Package _Framework :: Module Util :: Class memoize
[hide private]
[frames] | no frames]

Class memoize

source code

object --+
         |
        memoize


Decorator to use automatic memoization on a given function, such
that results are chached and, if called a second time, the
function will return the cached value. Example::

    @memoize
    def fibonacci(n):
        print "Computing fibonacci of:", n
        if n == 0:
            return 0
        if n == 1:
            return 1
        return fibonacci(n-1) + fibonacci(n-2)

    fibonacci(5)

If we removed the @memoize decorator, it would print O(2^n) lines
instead showing a exponential degeneration due to the binary
recursion.  However, already computed values will not recurse,
thus this program will print on the console:

    Computing fibonacci of: 5
    Computing fibonacci of: 4
    Computing fibonacci of: 3
    Computing fibonacci of: 2
    Computing fibonacci of: 1

Note that every computed value is cached in global state, so this
can be inapropiate when the function domain is very big.

Instance Methods [hide private]
 
__init__(self, function)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__call__(self, *args) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, function)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)