0

Setup (on Notebook / Colab)

# Install
!pip install callgraph  

# We will also use
from functools import lru_cache

%load_ext callgraph

In Python 3.x , if I do

@lru_cache(maxsize=128)
def factorial(n):
    return n * factorial(n-1) if n else 1

%callgraph factorial(3),factorial(5),factorial(7),factorial(9),

I get

enter image description here

But if I try not to use the decorator (because I may not have access to the function definition or don't want to be touching it).

def factorial(n):
    return n * factorial(n-1) if n else 1

lru_factorial = lru_cache(maxsize=None)(factorial)

%callgraph lru_factorial(3), lru_factorial(5), lru_factorial(7), lru_factorial(9)

I get

enter image description here

How do I combine %callgraph and non-decorator version of lru_cache to properly combine the results of the calls?

I even tried this

def fmix():
  return (factorial(3),factorial(5),factorial(7),factorial(9))


%callgraph lru_cache(maxsize=None)(fmix)

But it got worse.

enter image description here

Rub
  • 2,071
  • 21
  • 37

0 Answers0