我正在探索一个API,并使用以下方法来查看哪些方法是可用的,而不必用dir()
通过眼睛搜索所有属性:
methods = [m for m in dir(kt) if callable(getattr(kt, m))]
引发异常:
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
好吧,所以我不能用cross_fade
,那很好。但我没有试着打电话,我只是想知道我是否可以。你知道吗
我本以为callable()
可以使用类似于type()
的东西进行检查,但它似乎试图发出一个调用,只捕获某些类型的异常。你知道吗
我在终端中尝试type(kt.cross_fade)
和只尝试>>> kt.cross_fade
时得到相同的异常。你知道吗
所以我想这里有两个问题:callable
是否尝试呼叫?还有,什么会导致一个方法“存在”但完全不可用?你知道吗
callable
不尝试调用对象。它只检查对象是否有call操作符的实现。你知道吗尝试首先检索属性时发生异常。在Python中,属性访问可以被重写来做任何事情,并且这个对象的
cross_fade
属性被实现为一个property
,在getter上有一个only_on_master
修饰符,如果您试图在从属服务器上检索属性,这个修饰符会引发异常。你知道吗如果我们看一下source code,就会发现
cross_fade
实际上是一个属性:反过来,getter被包装到^{} 中,这是引发异常的地方。你知道吗
相关问题 更多 >
编程相关推荐