Python __call__ special method practical example Python __call__ special method practical example python python

Python __call__ special method practical example


This example uses memoization, basically storing values in a table (dictionary in this case) so you can look them up later instead of recalculating them.

Here we use a simple class with a __call__ method to calculate factorials (through a callable object) instead of a factorial function that contains a static variable (as that's not possible in Python).

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()

Now you have a fact object which is callable, just like every other function. For example

for i in xrange(10):                                                                 print("{}! = {}".format(i, fact(i)))# output0! = 11! = 12! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 362880

And it is also stateful.


Django forms module uses __call__ method nicely to implement a consistent API for form validation. You can write your own validator for a form in Django as a function.

def custom_validator(value):    #your validation logic

Django has some default built-in validators such as email validators, url validators etc., which broadly fall under the umbrella of RegEx validators. To implement these cleanly, Django resorts to callable classes (instead of functions). It implements default Regex Validation logic in a RegexValidator and then extends these classes for other validations.

class RegexValidator(object):    def __call__(self, value):        # validation logicclass URLValidator(RegexValidator):    def __call__(self, value):        super(URLValidator, self).__call__(value)        #additional logicclass EmailValidator(RegexValidator):    # some logic

Now both your custom function and built-in EmailValidator can be called with the same syntax.

for v in [custom_validator, EmailValidator()]:    v(value) # <-----

As you can see, this implementation in Django is similar to what others have explained in their answers below. Can this be implemented in any other way? You could, but IMHO it will not be as readable or as easily extensible for a big framework like Django.


I find it useful because it allows me to create APIs that are easy to use (you have some callable object that requires some specific arguments), and are easy to implement because you can use Object Oriented practices.

The following is code I wrote yesterday that makes a version of the hashlib.foo methods that hash entire files rather than strings:

# filehash.pyimport hashlibclass Hasher(object):    """    A wrapper around the hashlib hash algorithms that allows an entire file to    be hashed in a chunked manner.    """    def __init__(self, algorithm):        self.algorithm = algorithm    def __call__(self, file):        hash = self.algorithm()        with open(file, 'rb') as f:            for chunk in iter(lambda: f.read(4096), ''):                hash.update(chunk)        return hash.hexdigest()md5    = Hasher(hashlib.md5)sha1   = Hasher(hashlib.sha1)sha224 = Hasher(hashlib.sha224)sha256 = Hasher(hashlib.sha256)sha384 = Hasher(hashlib.sha384)sha512 = Hasher(hashlib.sha512)

This implementation allows me to use the functions in a similar fashion to the hashlib.foo functions:

from filehash import sha1print sha1('somefile.txt')

Of course I could have implemented it a different way, but in this case it seemed like a simple approach.