By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Apple" <ne**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Aug 1 '05 #2

P: n/a
Terry Reedy wrote:
"Apple" <ne**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 ...
Aug 1 '05 #3

P: n/a
"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 ;)

Aug 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.