English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If the object passed is displayed as callable, the callable() method will return True. If not, it will return False.
The syntax of callable():
callable(object)
The callable() method takes a single parameter object.
The callable() method returns:
True -If the object looks callable
False -If the object is not callable.
It is important to remember that even if callable() is True, the call to the object may still fail.
However, if callable() returns False, the call to the object will definitely fail.
x = 5 print(callable(x)) def testFunction(): print("Test") y = testFunction print(callable(y))
When running the program, the output is:
False True
Here, the objectxis not callable. And, the objectyIt seems to be callable (but may not be callable).
class Foo: def __call__(self): print('Print Something') print(callable(Foo))
When running the program, the output is:
True
The example of Foo class seems to be callable (in this case, it can be called).
class Foo: def __call__(self): print('Print Something') InstanceOfFoo = Foo() # Prints 'Print Something' InstanceOfFoo()
class Foo: def printLine(self): print('Print Something') print(callable(Foo))
When running the program, the output is:
True
The example of Foo class seems to be callable, but it cannot be called. The following code will raise an error.
class Foo: def printLine(self): print('Print Something') print(callable(Foo)) InstanceOfFoo = Foo() # Raises an error # The 'Foo' object is not callable InstanceOfFoo()