Testing methods
Ovid
ovid at easystreet.com
Sat Apr 19 11:52:53 EDT 2003
Jp Calderone wrote:
> Sometimes this is undesirable, but then the right answer depends on the
> particulars of why it is undesirable. There are other approaches, but none
> that work as reliably. For example:
>
> if hasattr(foo, 'some_method') and callable(foo.some_method):
> ...
>
> callable() may return false positives, though. I don't think it will ever
> return false negatives.
Thank you. I was actually just playing around with something like that.
The main problem is that the method name is determined at run time.
I've wound up with something like:
def can(object, method):
if not hasattr(object, method):
return 0
else:
return callable(getattr(object,method))
While that seems to work okay, you did mention that callable() can
return false positives. Can you give some examples?
Cheers,
Ovid
More information about the Python-list
mailing list