I'm new to algorithms, so I experimented with several features of algorithms, especially memoisation
I have a simple recursive function of a Fibonacci series using memoisation
class Memoize:
def __init__(self, f):
self.f = f
self.memo = {}
def __call__(self, *args):
if not args in self.memo:
self.memo[args] = self.f(*args)
print args
print self.memo
return self.memo[args]
def fibbo3(n):
if n==1:
return 1
elif n==0:
return 0
else:
return fibbo3(n-1) + fibbo3(n-2)
m = Memoize(fibbo3)
m(4)
print "check the memo"
print m.memo
(4,)
{(4,): 3}
check the memo
{(4,): 3}
But if I call
fibbo3 = Memoize(fibbo3)
fibbo3(4)
print 'ckeck the memo'
fibbo3.memo
(1,)
{(1,): 1}
(0,)
{(0,): 0, (1,): 1}
(2,)
{(2,): 1, (0,): 0, (1,): 1}
{(2,): 1, (0,): 0, (1,): 1}
(3,)
{(2,): 1, (0,): 0, (3,): 2, (1,): 1}
{(2,): 1, (0,): 0, (3,): 2, (1,): 1}
(4,)
{(2,): 1, (0,): 0, (3,): 2, (1,): 1, (4,): 3}
ckeck the memo
Out[524]:
{(0,): 0, (1,): 1, (2,): 1, (3,): 2, (4,): 3}
I see all the dictionary seen. Why does the variable name change from 'm' to 'fibbo3' (that is, the name of the function) leads to this behavior