# -*- coding: utf-8 -*-
# @File : builtin-callable-example.py
# @Date : 2018-09-20
# @Author : Peng Shiyu
# callable 函数, 检查一个对象是否可调用
# 对于函数, 方法, lambda 函式, 类, 实现了 __call__方法的类实例, 它都返回True.
def is_callable(function):
if callable(function):
print(function, "is callable")
else:
print(function, "is not callable")
class A(object):
def method(self, value):
return value
class B(A):
def __call__(self, value):
return value
a = A()
b = B()
is_callable(0)
is_callable("string")
is_callable(a)
is_callable(b)
is_callable(A)
is_callable(B)
is_callable(a.method)
"""
(0, 'is not callable')
('string', 'is not callable')
(<__main__.A object at 0x105e07190>, 'is not callable')
(<__main__.B object at 0x105e07390>, 'is callable')
(<class '__main__.A'>, 'is callable')
(<class '__main__.B'>, 'is callable')
(<bound method A.method of <__main__.A object at 0x105e07190>>, 'is callable')
"""