# Python function decorators
# try it - fib, fac
# timing?
# decorating! print, time, memoize...
import time
def dec42( f ):
def new_f( n ):
print "Let's decorate with n =", n, "!"
result = f( n )
print "Cleaning up decorations for n =", n, "."
return result
return new_f
def fib( n ):
if n == 0 or n == 1:
return 1
else:
return fib( n-1 ) + fib( n-2 )
"""
def timeme( f ):
def new_f( n ):
start_time = time.time()
result = f( n )
end_time = time.time()
print "Time taken for n =", n, "was", \
(end_time - start_time)
return result
return new_f
def memoize( f ):
Table = {}
def new_f( n ):
if Table.has_key( n ): return Table[n]
result = f( n )
Table[n] = result
return result
return new_f
"""