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.
Detects whether an object can be called
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.
>>> class A: #定义类A pass>>> Callable (a) #类A是可调用对象True >>> a = A () #调用类A >>> Callable (a) #实例a不可调用False >>> a () #调用实例a失败Traceback (most recent): File "<pyshell#31>", Line 1, in <module> A () TypeError: ' A ' object was not callable>>> class B: #定义类B def __call__ (self): C4/>print (' instances is callable now. ') >>> callable (b) #类B是可调用对象True >>> B = B () #调用类B >>> callable (b) #实例b是可调用对象True >>> B () #调用实例b成功instances is callable now.
Python built-in function (--callable)