Python内建函数-callable

Python内建函数-callable

callable(object)

 

中文说明:检查对象object是否可调用。如果返回True,object仍然可能调用失败;但如果返回False,调用对象ojbect绝对不会成功。

注意:类是可调用的,而类的实例实现了__call__()方法才可调用。

版本:该函数在python2.x版本中都可用。但是在python3.0版本中被移除,而在python3.2以后版本中被重新添加。

 

英文说明: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); class instances are callable if they have a __call__() method.

 

注:callable()函数用于测试对象是否可调用,如果可以则返回True(真);否则返回False(假)。可调用对象包括函数、方法、代码对象、类和已经定义了 调用(__call__()) 方法的类实例。

[python] view plain copy
 
    1. >>> callable(0)  
    2. False  
    3. >>> callable("mystring")  
    4. False  
    5. >>> def add(a, b):  
    6. …     return a + b  
    7. …  
    8. >>> callable(add)  
    9. True  
    10. >>> class A:  
    11. …      def method(self):  
    12. …         return 0  
    13. …  
    14. >>> callable(A)  
    15. True  
    16. >>> a = A()  
    17. >>> callable(a)  
    18. False  
    19. >>> class B:  
    20. …     def __call__(self):  
    21. …         return 0  
    22. …  
    23. >>> callable(B)  
    24. True  
    25. >>> b = B()  
    26. >>> callable(b)  
    27. True  
posted @ 2017-12-21 16:59  菜鸡一枚  阅读(182)  评论(0)    收藏  举报