Alex Rivera | Logout

Python: variables scope and profile.run

Asked 2011-12-30T18:32:40.587
11

I want to call profile.run within my function, i.e.:

def g():
    ...
def f():
    x = ...
    run.profile('g(x)')

However, it says 'x is not defined' when calling run.profile. As far as I understand, I have to supply import statement before calling g(x) inside string argument to run.profile, and I could do this with global variables.

Is this possible with local variables?

Edit
Report

1 Answer

2

Instead of having x set to the arguments to the function, have x include the entire function call.

Example:

import cProfile
def g(x):
    print x
x = """g("Hello world!")"""
cProfile.run(x)
answered 2011-12-30T18:38:55.563

Your Answer