Python 内建函数 - callable(object)

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值