introspection?

Roman Suzi rnd at onego.ru
Thu Aug 19 06:09:49 CEST 2004


On Wed, 18 Aug 2004, Jim Benson wrote:

I am using different approach to introspection of Python
functions and methods (not all available info output,
only name of the previously called function/method).
I am not sure if this is better than getting sys._getframe()
or not, at least it doesn't use undescore methods ;-)
and "knowns" about all invocation history at once,
could even show source code, etc.

import traceback
def Introspec(msg):
   print traceback.extract_stack()[-2][2] + ":", msg

class AAA:
   def foo(self):
      Introspec("i'm running")

def a(x):
   Introspec('i am running')
   aaa = AAA()
   aaa.foo()

def b(x):
   Introspec('I am running')
   a(x)

b(10)



>On Wed, 18 Aug 2004, Troy Melhase wrote:
>
>> import sys
>> def F():
>>     print sys._getframe().f_code.co_name
>> >>> F()
>> F
>
>ooooh...thanks Troy!
>indeed that works fine for getting the method name.
>
>Thanks to all...and apologies for taking the easy way out
>by posting to this very helpful list rather than
>doing more research with google (actually i did
>do a search for 'python class method introspection'
>before posting...i didn't look at all the returns).
>
>Thanks,
>
>Jim
>
>
>
>
>

Sincerely yours, Roman Suzi
-- 
rnd at onego.ru =\= My AI powered by GNU/Linux RedHat 7.3



More information about the Python-list mailing list