English documents:
callable
(object)
Return True
If the object argument appears callable, False
if not. If this returns true, it's still possible that a call fails and if it is false, calling object would never Succe Ed. Note That classes is callable (calling a class returns a new instance); Instances is callable if their class has a __call__()
method.
Description
1. Method is used to detect whether an object can be called, which can be called by means of a method call that the object can use () parentheses.
>>> Callable (callable)
True
>>> Callable (1)
False
>>> 1 ()
Traceback (most recent):
File "<pyshell#5>", line 1, in <module>
1 ()
TypeError: ' int ' object is not callable
>>>
2. Callable objects may also fail to invoke the actual call, but cannot invoke the object, and the invocation must not succeed.
3. Class objects are callable objects, whether an instance object of a class can invoke an object, depending on whether the class defines the __call__ method.
>>>classA:#Defining Class A Pass>>> Callable (A)#Class A is a callable objectTrue>>> a = A ()#Call Class A>>> Callable (a)#instance A is not callableFalse>>> A ()#failed to invoke instance aTraceback (most recent): File"<pyshell#31>", Line 1,inch<module>A () TypeError:'A'Object is notcallable>>>classB:#Defining Class B def __call__(self):Print('instances is callable now.') >>> Callable (B)#Class B is a callable objectTrue>>> B = B ()#Call Class B>>> Callable (b)#Instance B is a callable objectTrue>>> B ()#Invoking instance B succeedsInstances is callable now.
Python built-in functions (9)--callable