Python callable() Built in Function
The Python callable() function returns True if the specified object is callable, otherwise it returns False. A callable object is an object that can be invoked using the () operator, such as functions, methods, classes, etc. The callable() function checks if the object has a call() method.
Here are some examples of using callable() function:
# Example 1: Check if a function is callable
def add(x, y):
return x + y
print(callable(add)) # Output: True
# Example 2: Check if a class is callable
class Person:
def __init__(self, name):
self.name = name
print(callable(Person)) # Output: True
# Example 3: Check if an instance of a class is callable
p = Person("Alice")
print(callable(p)) # Output: False
# Example 4: Check if a lambda expression is callable
square = lambda x: x * x
print(callable(square)) # Output: True
# Example 5: Check if a built-in function is callable
print(callable(print)) # Output: True
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment