Gandalf wrote:
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-...built-in-funcs
-Peter