How-to : Running profile from inside an class on the class' own methods ??

Thomas Weholt thomas at weholt.org
Sun Mar 17 14:52:41 EST 2002


Hi,

I want to profile a method inside a class from another method inside the
same class. Example code below :

>>> class A:
...  def __init__(self):
...   self.var1 = 'var1'
...  def go(self):
...   self.var1 = 'go1'
...  def profile(self):
...   import profile
...   profile.run('self.go()')
...
>>> a = A()
>>> a.profile()
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 8, in profile
  File "E:\Python21\lib\profile.py", line 71, in run
    prof = prof.run(statement)
  File "E:\Python21\lib\profile.py", line 356, in run
    return self.runctx(cmd, dict, dict)
  File "E:\Python21\lib\profile.py", line 362, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in ?
NameError: name 'self' is not defined
>>>

How can I profile the go-method inside the A-class from inside the A-class
?? Any hints?

Thomas





More information about the Python-list mailing list