0

I do not understand what exactly happening with the 'memory' dictionary when I call fibonacci(n - 1) and fibonacci(n - 1) in line 8. My understanding is if I do not specify the keyword argument when the function is called, it will take the default value which is an empty dictionary. But somehow it is not setting memory to an empty dictionary but instead, it has access to the current values.

Thanks in advance

def fibonacci(n, memory = {}):
    m = n - 1
    if m in memory:
        return memory[m]
    elif m <= 1:
        return 1
    else:
        memory[m] = fibonacci(n - 2) + fibonacci(n - 1)
        return memory[m]
G.D.Z
  • 1
  • 1
  • 2
    https://stackoverflow.com/questions/55068876/recursion-memoization-and-mutable-default-arguments-in-python – Tom Dalton Jul 16 '21 at 18:21

0 Answers0