a=5
x="hi"
my_list=['One','Two','Three']
print(callable(a)) # False
print(callable(x)) # False
print(callable(my_list)) # False
All above objects are not Callable.
Using Class and Objects
class student_kit():
#instance attributes
def __init__(self,name):
self.name= name
Alex=student_kit('Alex') # object declaration
print(callable(student_kit)) # True
print(callable(Alex)) # False , object is not callable
#Alex() # this will generate error
The last line will generate error like this.
TypeError: 'student_kit' object is not callable
The class student_kit is callable, the object Alex is not callable.
def my_function():
print("Hello")
print(callable(my_function)) # Output: True
x = 5
print(callable(x)) # Output: False
class MyClass:
def method(self):
print("This is a method.")
obj = MyClass()
print(callable(obj.method)) # Output: True
print(callable(obj)) # Output: False
class CallableClass:
def __call__(self):
print("This class instance is callable.")
obj = CallableClass()
print(callable(obj)) # Output: True
string = "Hello"
print(callable(string)) # Output: False
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.