Python magic methods (2)

Posted by All about Python on February 7, 2017

Python magic methods (2) : __call__()

In Python, we can call a method using object.method(). Is it possible if we use object() to call a method or do something? __call__() method can help us to achieve this - by defining __call__(), the class’s instance can be called as a function.

Example:

class Person(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print('Name is %s.' % self.name)

if __name__ == '__main__':
    p = Person('mike')
    print p()

Result:

Name is mike.

When to use __call__()?

If we need the objects to be callable, or the object wraps, abstracts the concept of a function, then use __call__().

Example:

class  Factorial:    
    def  __init__( self ):    
        self .cache = {}    
          
    def  __call__( self , n):    
        if  n  not  in  self .cache:    
            if  n ==  0:    
                self .cache[n] =  1    
            else:    
                self .cache[n] = n *  self .__call__(n-1)    
        return  self .cache[n]    
    
fact = Factorial()    
    
for  i  in  xrange(10):                                                                 
    print ( "{}! = {}" .format(i, fact(i)))  

Result:

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880