Manual
Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.
直译
如果object参数可调用则返回True,否则False。如果返回真,参数仍有可能调用失败,如果否,调用对象则永远不会成功。注意类是可调用的(调用一个类将返回一个新的实例);如果类有__call__() 方法,则它们的实例可调用。
实例
数字(值)
>>> callable(17)
False
字符串
>>> callable('CSDN')
False
迭代
>>> callable(range(10))
False
函数
>>> def csdn(x):
t = x
for i in range(10):
t += i
return t
>>> csdn(2)
47
>>> callable(csdn(2))
False
类(有__call__() 方法)
>>> class CSDN:
def __call__(self):
return 47
def csdn(self, x):
t = x
for i in range(10):
t += i
return t
>>> c = CSDN()
>>> c.csdn(2)
47
>>> callable(c)
True
>>> callable(CSDN)
True