Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python profiling an inner function

I am trying to profile a part of my program. The pattern is like the following:

def de():
      def abc():
         print("123")
      cProfile.run('abc()')

When I am trying to run this program, I got an error: File "", line 1, in NameError: name 'abc' is not defined

Is there anyway to work around this error?

like image 273
Zack Avatar asked Jun 09 '26 02:06

Zack


1 Answers

Everything happening outside of the function, meaning that they are hidden from the global scope.

use runctx(). Please read https://docs.python.org/3/library/profile.html#profile.runctx

import cProfile

def de():
      def abc():
         print("123")
      cProfile.runctx('abc()', None, locals=locals())

de()

output:

"123"
5 function calls in 0.000 seconds
like image 119
sandes Avatar answered Jun 12 '26 11:06

sandes