has_method
John Lenton
john at grulic.org.ar
Tue Aug 31 17:24:42 EDT 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.
try this:
method = getattr(a, 'a', None)
if callable(method):
method(args)
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...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20040831/717dd770/attachment.sig>
More information about the Python-list
mailing list