Will Will - 3 years ago 58
Python Question

Does callable(obj) make an attempt to call?

I am exploring an API, and using the following to see what methods are available without searching through all attributes by eye with


methods = [m for m in dir(kt) if callable(getattr(kt, m))]

which throws an exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/soco/core.py", line 103, in inner_function
raise SoCoSlaveException(message)
soco.exceptions.SoCoSlaveException: The method or property "cross_fade" can only be called/used on the coordinator in a group

Ok, so I can't use
, that's fine. But I didn't try to call it, I was just trying to find out if I could.

I would have thought that
would be able to check using something similar to
, but it seems like it's attempting to make a call and only catching certain types of exceptions.

I get the same exception when I tried
, and when I just try
>>> kt.cross_fade
in the terminal.

So I guess there are two questions here: Does
make an attempt to call? And, what can cause a method to "exist" but be completely unavailable?

Answer Source

callable doesn't attempt to call the object. It only checks that the object has an implementation of the call operator.

The exception is occurring in the attempt to retrieve the attribute in the first place. Attribute access can be overridden to do just about anything in Python, and the cross_fade attribute of this object is implemented as a property with an only_on_master decorator on the getter that raises an exception if you try to retrieve the attribute on a slave.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download