Re: introspection?

Discussion in 'Python' started by Roman Suzi, Aug 19, 2004.

  1. Roman Suzi

    Roman Suzi Guest

    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
    --
    =\= My AI powered by GNU/Linux RedHat 7.3
     
    Roman Suzi, Aug 19, 2004
    #1
    1. Advertising

  2. Roman Suzi

    David Bolen Guest

    Roman Suzi <> writes:

    > 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.


    Note that for Python 2.1+ you can use the inspect module's
    "currentframe" method to obtain your current frame in lieu of
    sys._getframe(), if you prefer a more documented approach.

    I believe you should also be able to work your way back up the call
    chain using the f_back field within any given frame object if you want
    to.

    -- David
     
    David Bolen, Aug 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Smith
    Replies:
    0
    Views:
    323
    David Smith
    Jul 11, 2003
  2. John Harrison

    Re: class introspection

    John Harrison, Jul 23, 2003, in forum: C++
    Replies:
    4
    Views:
    376
    John Harrison
    Jul 24, 2003
  3. Steven T. Hatton

    Introspection Guidelines for C++?

    Steven T. Hatton, Sep 3, 2004, in forum: C++
    Replies:
    0
    Views:
    617
    Steven T. Hatton
    Sep 3, 2004
  4. Steven T. Hatton

    Toward an Introspection Open Standard

    Steven T. Hatton, Oct 25, 2004, in forum: C++
    Replies:
    4
    Views:
    449
    Jonathan Turkanis
    Oct 25, 2004
  5. David C. Fox
    Replies:
    5
    Views:
    348
    Steven Taschuk
    Jul 23, 2003
Loading...

Share This Page