您的位置:首页 > 移动开发 > Objective-C

Python 内建函数 - callable(object)

2017-03-13 15:08 513 查看
Manual

直译

实例
数字值

字符串

迭代

函数

类有__call__ 方法

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python