Skip to main content

Python Class Call

 https://www.geeksforgeeks.org/__call__-in-python/


__call__ in Python


object() is shorthand for object.__call__()



class Example: 

    def __init__(self): 

        print("Instance Created") 

      

    # Defining __call__ method 

    def __call__(self): 

        print("Instance is called via special method") 

  

# Instance created 

e = Example() 

  

# __call__ method will be called 

e() 



Output :


Instance Created

Instance is called via special method



class Product: 

    def __init__(self): 

        print("Instance Created") 

  

    # Defining __call__ method 

    def __call__(self, a, b): 

        print(a * b) 

  

# Instance created 

ans = Product() 

  

# __call__ method will be called 

ans(10, 20) 



Output :


Instance Created

200