has_method

Steve Holden sholden at holdenweb.com
Tue Aug 31 09:16:02 EDT 2004


Gandalf wrote:
> 
>  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



More information about the Python-list mailing list