Home > Backend Development > Python Tutorial > python function - callable(object)

python function - callable(object)

高洛峰
Release: 2016-10-17 15:33:55
Original
1256 people have browsed it

callable(object)

Chinese description: Check whether the object object is callable. If True is returned, object may still fail to be called; but if False is returned, calling object object will never succeed.

Note: Classes are callable, and instances of the class can only be called if they implement the __call__() method.

Version: This function is available in python2.x version. However, it was removed in python3.0 and added again in python3.2 and later versions.


English description: 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.


Code example:

>>> callable(0)
False
>>> callable("mystring")
False
>>> def add(a, b):
…     return a + b
…
>>> callable(add)
True
>>> class A:
…      def method(self):
…         return 0
…
>>> callable(A)
True
>>> a = A()
>>> callable(a)
False
>>> class B:
…     def __call__(self):
…         return 0
…
>>> callable(B)
True
>>> b = B()
>>> callable(b)
True
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template