has_method

G

Gandalf

Hi All!

Does anyone knows how to tell if an object has a method with a given
name? How can I access that method?

For attributes, it is easy:

class A(object):
a = 12
b = 'Python'

a = A()
a.__dict__.has_key('a') # True
a.__dict__.has_key('b') # True
a.__dict__.has_key('c') # False

But it won't work for methods. Thanks in advance.

Laci 2.0
 
S

Steve Holden

Gandalf said:
Hi All!

Does anyone knows how to tell if an object has a method with a given
name? How can I access that method?

For attributes, it is easy:

class A(object):
a = 12
b = 'Python'

a = A()
a.__dict__.has_key('a') # True
a.__dict__.has_key('b') # True
a.__dict__.has_key('c') # False

But it won't work for methods. Thanks in advance.

Laci 2.0
Well, one way is try/except:

try:
a.someMethod(arg1, arg2)
except AttributeError:
print "Aarrgghh!"

regards
Steve
 
P

Peter Hansen

Gandalf said:
Does anyone knows how to tell if an object has a method with a given
name? How can I access that method?

For attributes, it is easy:

class A(object):
a = 12
b = 'Python'

a = A()
a.__dict__.has_key('a') # True
a.__dict__.has_key('b') # True
a.__dict__.has_key('c') # False

But it won't work for methods. Thanks in advance.

Don't access __dict__ directly. In fact, most of the time
the presence of the __ underscores is to warn you that you
are doing something unusual... here's the better way:

if callable(getattr(a, 'b')):
print 'has a method called b'
else:
print 'no method called b'

getattr(obj, name) retrieves the attribute, and callable()
checks if it's a method (roughly speaking... not quite but
good enough for your purposes I believe). Look in the docs
for the __builtin__ module to learn about these and other
such useful functions.
http://docs.python.org/lib/built-in-funcs.html#built-in-funcs

-Peter
 
G

Gandalf

Well, one way is try/except:

try:
a.someMethod(arg1, arg2)
except AttributeError:
print "Aarrgghh!"
But since I have my method name in a variable, it will be this way:

methods = dir(self)
if methodname in methods:
cmd = "self." + methodname
method = eval(cmd)

This is what I wanted to avoid - i.e. the use of eval. There must be an
easier and quicker way to do this.
 
P

Peter Otten

Gandalf said:
But since I have my method name in a variable, it will be this way:

methods = dir(self)
if methodname in methods:
cmd = "self." + methodname
method = eval(cmd)

This is what I wanted to avoid - i.e. the use of eval. There must be an
easier and quicker way to do this.

method = getattr(self, methodname, None)
if method:
method(arg1, arg2)

Note that this may still fail if the attribute's value is not callable (and
bool(method) True).
Use

try:
method(arg1, arg2)
except TypeError:
pass

to guard against that.

Peter
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top