import profile def foo(bar=None): print("bar = %s" % bar) profile.runctx("foo", globals(), {'bar': 'good'}) profile.run("foo('great')")