2

In jupyter Notebook, I am trying to use %lprun on nested functions but I do not sucess.

The following code, in a notebook cell

def outerfoo():
    def innerfoo():
        print("Hello World")
        print("Good Bye")
    innerfoo()

%lprun -f outerfoo().innerfoo outerfoo()

outputs the messages (Hello World and GoodBye) but after I have this error :
UsageError: Could not find function 'outerfoo().innerfoo'.
AttributeError: 'NoneType' object has no attribute 'innerfoo'

And this one,

def outerfoo():
    def innerfoo():
        print("Hello World")
        print("Good Bye")
    %lprun -f innerfoo innerfoo()


outerfoo()

does not print messages and gives this error :
UsageError: Could not find function 'innerfoo'. NameError: name 'innerfoo' is not defined

How is it possible to profile innerfoo ?

Stef1611
  • 1,978
  • 2
  • 11
  • 30

1 Answers1

2

I don't think you can use notebook magic inside of actual Python code, so using %lprun inside of a function block will likely never work. Instead you can store a global reference to your local function, then lprun that:

ref = None
def outerfoo():
    global ref
    def innerfoo():
        print("Hello World")
        print("Good Bye")
    innerfoo()
    ref = innerfoo

# Must be called for `ref` to exist.
outerfoo()

%lprun -f ref ref()

But all of this feels very icky. You probably shouldn't be creating nested functions in the first place.

Robin De Schepper
  • 4,942
  • 4
  • 35
  • 56
  • 1
    Thanks a lot for the trick. I agree with your remark *you shouldn't be creating nested functions in the first place*. But I am profiling a large code that I have not written and modifying it is a considerable work. – Stef1611 Dec 10 '21 at 11:01
  • 2
    If I transpose your answer to my real problem, `innerfoo` is called many times. So I use `%lprun -f ref outerfoo()` And it works perfectly even if it is very icky. – Stef1611 Dec 10 '21 at 11:09
  • I already had a feeling like that :) Just wanted to make sure to mention it isn't good practice :D – Robin De Schepper Dec 10 '21 at 13:05
  • I followed the exact same steps but didn't show the Line Contents saying "Are you sure you are running this program from the same directory that you ran the profiler from?" Continuing without the function's contents. What could be the reason? Because just seeing #hits, #time without seeing which line is useless then. – knowledge_seeker Jun 22 '22 at 18:17