Accessing method object from within a method

Discussion in 'Python' started by Vivek Sawant, Feb 18, 2004.

  1. Vivek Sawant

    Vivek Sawant Guest

    Hi,

    Is there a way to obtain the method/function object from within the
    method like 'self' refers to the object instance. I would like to avoid
    looking method name (string) in class/object attributes. My goal is to
    write code that need not be changed when the method name is changed.

    def method (self, ...)
    # somehow obtain 'methodobj' for this 'method'
    mname = methodobj.__name__;
    # do something with mname

    Thanks.
    \vivek
    Vivek Sawant, Feb 18, 2004
    #1
    1. Advertising

  2. Vivek Sawant

    Peter Hansen Guest

    Vivek Sawant wrote:
    >
    > Is there a way to obtain the method/function object from within the
    > method like 'self' refers to the object instance. I would like to avoid
    > looking method name (string) in class/object attributes. My goal is to
    > write code that need not be changed when the method name is changed.
    >
    > def method (self, ...)
    > # somehow obtain 'methodobj' for this 'method'
    > mname = methodobj.__name__;
    > # do something with mname


    The usual use for this is for debugging purposes, when somebody wants
    to print the name of the current method. If your needs are not much
    different than that, look into sys._getframe() and frame objects.

    -Peter
    Peter Hansen, Feb 18, 2004
    #2
    1. Advertising

  3. Vivek Sawant

    Vivek Sawant Guest

    Peter,

    I had browsed through documentation for frame objects earlier, but
    nothing had jumped out. Here's what your suggestion helped me conjur up
    and it worked:

    def method (self, ...)
    mname = sys._getframe(0).f_code.co_name;
    # do something with mname

    Is this what you had in mind or did you mean to suggest something better?

    Thanks!
    \vivek


    Peter Hansen wrote:

    >Vivek Sawant wrote:
    >
    >
    >>Is there a way to obtain the method/function object from within the
    >>method like 'self' refers to the object instance. I would like to avoid
    >>looking method name (string) in class/object attributes. My goal is to
    >>write code that need not be changed when the method name is changed.
    >>
    >>def method (self, ...)
    >> # somehow obtain 'methodobj' for this 'method'
    >> mname = methodobj.__name__;
    >> # do something with mname
    >>
    >>

    >
    >The usual use for this is for debugging purposes, when somebody wants
    >to print the name of the current method. If your needs are not much
    >different than that, look into sys._getframe() and frame objects.
    >
    >-Peter
    >
    >
    Vivek Sawant, Feb 18, 2004
    #3
  4. Vivek Sawant

    Peter Hansen Guest

    > Vivek Sawant wrote:
    >
    > I had browsed through documentation for frame objects earlier, but nothing
    > had jumped out. Here's what your suggestion helped me conjur up and it worked:
    >
    > def method (self, ...)
    > mname = sys._getframe(0).f_code.co_name;
    > # do something with mname
    >
    > Is this what you had in mind or did you mean to suggest something better?


    That's about it. By referring to frame objects, I meant to lead you to
    the stuff which you found so that you could see the extent of information
    aside from just function name, which is available to you. Enjoy! :)

    -Peter
    Peter Hansen, Feb 18, 2004
    #4
    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. Damon Getsman
    Replies:
    3
    Views:
    707
    Nigel Wade
    Sep 24, 2008
  2. Kenneth Baltrinic

    Accessing Server.MapPath from within Custom Controls Render() method?

    Kenneth Baltrinic, Oct 16, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    334
    Rajesh.V
    Oct 16, 2003
  3. Elf M. Sternberg
    Replies:
    15
    Views:
    269
    Matthias Reitinger
    Jul 29, 2009
  4. Martin
    Replies:
    6
    Views:
    180
    Richard Cornford
    Aug 20, 2004
  5. Replies:
    2
    Views:
    76
Loading...

Share This Page