Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

class __call__ method python

# __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
 
PREVIOUS NEXT
Tagged: #class #method #python
ADD COMMENT
Topic
Name
7+1 =