Python callable () function usage instance analysis, pythoncallable
This example describes the usage of the Python callable () function. We will share this with you for your reference. The details are as follows:
Built-in functions in pythoncallable( ), YesCheck whether an object is callable.
For functions, methods, lambda functions, classes, and Implementations_ _call_ _Method class instance, which returns True.
>>> help(callable)Help on built-in function callable in module __builtin__:callable(...) callable(object) -> bool Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances with a __call__() method.
1. functions are callable.
>>> def add(x,y):... return x+y...>>> callable(add)True
2. Class and Class methods are callable
>>> Class C:... def printf (self):... print 'This is class C! '... >>> ObjC = C () >>> callable (C) # classes are callable. When you call them, a corresponding class instance is generated. true >>> callable (C. printf) True >>> callable (objC. printf) True
3.__call__()Method class instances are callable
>>> Class A:... def printf (self):... print 'This is class! '... >>> ObjA = A () >>> callable (A) # classes are callable. When you call them, A corresponding class instance is generated. true >>> callable (objA) # Class A does not implement the _ call _ () method. Therefore, an instance of class A is not callable, False >>> class B :... def _ call _ (self ):... print 'this is class B! '... >>> ObjB = B () >>> callable (B) # classes are callable. When you call them, a corresponding class instance is generated. true >>> callable (objB) # Class B implements the _ call _ () method. Therefore, Class B instances are callable to True.
4. lambda expressions are callable
>>> f=lambda x,y:x+y>>> f(2,3)5>>> callable(f)True
Others, such as integers, strings, lists, tuples, and dictionaries, are not callable, as shown below:
>>> callable(2)False>>> callable('python')False>>> l=[1,2,3]>>> callable(l)False>>> t=(4,5,6)>>> callable(t)False>>> d={'a':1,'b':2}>>> callable(d)False