Python built-in functions (9) -- callable, pythoncallable
English document:
callable
(Object)
ReturnTrue
IfObjectArgument appears callable,False
If not. If this returns true, it is still possible that a call fails, but if it is false, callingObjectWill never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has__call__()
Method.
Note:
1. The method is used to check whether an object can be called. The call indicates whether the object can be called using the () method.
>>> Callable (callable)
True
>>> Callable (1)
False
>>> 1 ()
Traceback (most recent call last ):
File "<pyshell #5>", line 1, in <module>
1 ()
TypeError: 'int' object is not callable
>>>
2. callable objects may also fail to be called in actual cases. However, if an object cannot be called, the call will certainly fail.
3. All class objects are callable objects. Whether the class object can be called depends on whether the class defines the _ call _ method.
>>> Class A: # define class A pass >>> callable (A) # class A is a callable object True >>> A = () # call Class A >>> callable (a) # instance a cannot call False >>> a () # call instance a failed Traceback (most recent call last ): file "<pyshell #31>", line 1, in <module> a () TypeError: 'A' object is not callable >>> class B: # define class B def _ call _ (self): print ('instances' are callable now. ') >>> callable (B) # Class B is the callable object True >>> B = B () # Call Class B >>> callable (B) # instance B is the callable object True> B () # instances are callable now.