introspection?

Discussion in 'Python' started by Jim Benson, Aug 18, 2004.

  1. Jim Benson

    Jim Benson Guest

    newbie question here:

    How does one get the class and method name from within
    a method? i.e here i have obviously hardcoded the class
    and method name:

    class JJTest:

    def methodA(self):

    print 'ERROR: in JJTest.methodA'

    >>> from jjtest import JJTest
    >>> jj = JJTest()
    >>> jj.methodA()

    ERROR: in JJTest.methodA
    >>>


    The Java way of doing this would be something like:

    System.out.println("ERROR in: " + this.getClass().getName());

    I suspect that there are attributes that one can use to do
    something similar in Python.

    If any of you Python experts would let me know how to do this in
    Python, i would appreciate it.

    Thanks,

    Jim
     
    Jim Benson, Aug 18, 2004
    #1
    1. Advertising

  2. Jim Benson

    Neal Norwitz Guest

    Jim Benson <> wrote in message news:<>...
    >
    > How does one get the class and method name from within
    > a method?


    >>> import sys
    >>> class C:

    ... def foo(self):
    ... print self.__class__.__name__, sys._getframe().f_code.co_name
    ...
    >>> C().foo()

    C foo

    Neal
     
    Neal Norwitz, 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:
    320
    David Smith
    Jul 11, 2003
  2. John Harrison

    Re: class introspection

    John Harrison, Jul 23, 2003, in forum: C++
    Replies:
    4
    Views:
    371
    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:
    608
    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:
    440
    Jonathan Turkanis
    Oct 25, 2004
  5. David C. Fox
    Replies:
    5
    Views:
    346
    Steven Taschuk
    Jul 23, 2003
Loading...

Share This Page