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

A

Apple

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

Terry Reedy

Apple said:
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
 
J

John Machin

Terry said:
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 ...
 
A

Apple

"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 ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top