Callable in Python

 

Code


The callable() takes an object as Input and then It returns boolean value as an Output.

Now, If the object you are passing is callable then it will return True otherwise it will return False


As we all know function is callable like we a function and we can call it anywhere.

Example 1:

def demo_function_example():

      print("Hey I am with in Function")


Now if you want to check the above function is callable or not? see the below code sample

>>>callable(demo_function_example)

Output:  True


Similarly for a class

Example 2 :

class DemoClass:

         pass


>>>callable(DemoClass)

Output: True


But if we create one object and test the is it callable or not ? we will get False

>>> obj = A()

>>> callable(obj)

False


So, to make the object callable we need to create one __call__ method within that class. See the below code


>>> class DemoClass:

def __call__(self):

print("I am Now Callable")

pass

>>> obj = DemoClass()

>>> callable(obj)

True



Quiz Level 1

Quiz Level 2


Request Me



Comments

Popular posts from this blog

How To Create .ENV File in Python

How to solve the Liner Equation using Python

Create a Large file for Test Data Processing using Python