Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function attribute used in recursion – does a global function attribute exist?

I have a function:

def fib(n, fib_d = {}):
     
   if n < 2:
      return 1
    
   if n not in fib_d:
     fib_d[n] = fib(n-2) + fib(n-1)
    
 
   return fib_d[n]

fib(1000)
# a big number returned in an instance

Then I try to create fib_d as an attribute of a function which data I could access:

def fib(n):
   fib.fib_d = {}
   
   if n < 2:
      return 1
    
   if n not in fib.fib_d:
     fib.fib_d[n] = fib(n-2) + fib(n-1)
    
 
   return fib.fib_d[n]
fib(10)
# returns 89 in an instance
fib(100)
# runs till I stop it

So I am guessing that fib_d is being created each time as an empty parameter, hence the slow calculation

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am wondering if I can have a global function attribute so that I could access it’s content like:

fib.fib_d
# {2: 2, 3: 3, 4: 5, 5: 8, 6: 13, 7: 21, 8: 34, 9: 55, 10: 89, ..., n: m}

I tried:

def fib(n, fib.fib_d):
   ...

But that’s not valid, I understand that probably functions attribute should be bound only to that function but I am curious if there is such thing as global function attributes or at least attribute that can be used in a recursion.

>Solution :

I would just use a cache, or lru_cache.

But to do it like you’ve asked for, the easiest way to do this is to define your function within a closure:

def make_fib():

   def fib(n):
   
       if n < 2:
          return 1
    
       if n not in fib.fib_d:
         fib.fib_d[n] = fib(n-2) + fib(n-1)
       return fib.fib_d[n]
   fib.fib_d = {}
   return fib

f= make_fib()
f(2000)

That way, we have an attribute of the function properly scoped.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading