Determining method type given its string name presentation and its corresponding object reference.

Discussion in 'Python' started by Apple, Aug 1, 2005.

  1. Apple

    Apple Guest

    Hi I am a bit new to python. I was wondering if there is a way to
    determine whether or not a given string is a member method of a given
    object:

    def is_a_method(self, attr_name):
    'returns True if attr_name is an instance method of self; false
    otherwise'



    The problem is that I have overridden __repr__ in such a way that it's
    not self-contained. It makes a call to a member method, call it m(),
    that would call up getattr() which eventually makes another call back
    to __repr__:

    Example output:
    (<bound method Class.attr_name of "INSTANCE'S __repr__() HERE">).


    So, I want to trap all attr_name that are methods comeing into m() and
    make sure they're not passed on to getatrr() i.e. making __repr__
    self-contained (hopefully).

    Is there a simple way to determine a given attr_name is_a_method()?
    Thanks in advance for all your help.
    Apple, Aug 1, 2005
    #1
    1. Advertising

  2. Apple

    Terry Reedy Guest

    Re: Determining method type given its string name presentation anditscorresponding object reference.

    "Apple" <> wrote in message
    news:...
    > Hi I am a bit new to python. I was wondering if there is a way to
    > determine whether or not a given string is a member method of a given
    > object:


    Does callable(cls.attr) fit your needs?

    Terry J. Reedy
    Terry Reedy, Aug 1, 2005
    #2
    1. Advertising

  3. Apple

    John Machin Guest

    Re: Determining method type given its string name presentation anditscorresponding object reference.

    Terry Reedy wrote:
    > "Apple" <> wrote in message
    > news:...
    >
    >>Hi I am a bit new to python. I was wondering if there is a way to
    >>determine whether or not a given string is a member method of a given
    >>object:

    >
    >
    > Does callable(cls.attr) fit your needs?
    >


    or perhaps callable(getattr(obj, strg, None)) if the need is related to
    an unknown string which may be a callable attribute of an object whose
    class is unknown ...
    John Machin, Aug 1, 2005
    #3
  4. Apple

    Apple Guest

    Re: Determining method type given its string name presentation and itscorresponding object reference.

    "or perhaps callable(getattr(obj, strg, None)) if the need is related
    to
    an unknown string which may be a callable attribute of an object whose
    class is unknown ..."

    Hm.. the problem is I cannot call getattr() at all. Because getattr()
    will call my __repr__() which will call another m() that will
    eventually call getattr() again.. It's an infinite recursion. In
    general, I think every time you try to qualify a method attribute, e.g.
    obj.method, it'd actually end up making a call to __repr__()...

    obj.method would usually get you this output:
    (<bound method Class_of_obj.method of "INSTANCE'S __repr__() CALL
    HERE">)

    More or less I am trying to understand what getattr did to figure out
    the name "method" is actually a method (bounded or not). This way I
    can avoid the recursive call to __repr__(). I hope this clarifies my
    question a bit?

    Thanks a lot for the help ;)
    Apple, Aug 1, 2005
    #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.

Share This Page