#FROM GEEKSFORGEEKS <3
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() #prints "Instance is called via special method"
# __call__ special function
# __call__() special method allows class instances to behave like functions
# i.e. can be called like a function - this is useful for class decorators
class Product:
def __init__(self):
print('Instance created')
def __call__(self, a=5, b=10):
print(a * b)
ans = Product()
# Instance created
ans(10, 20) # __call__ method will be called here
# 200
ans()
# 50