john at grulic.org.ar
Tue Aug 31 23:24:42 CEST 2004
On Tue, Aug 31, 2004 at 03:13:23PM +0200, 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.
method = getattr(a, 'a', None)
also consider using getattr with a default, or just plain hasattr,
instead of accessing the dict directly. Accessing __dict__ like that
is a bad idea with few exceptions.
John Lenton (john at grulic.org.ar) -- Random fortune:
Writing is turning one's worst moments into money.
-- J.P. Donleavy
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 196 bytes
Desc: Digital signature
More information about the Python-list