[Django]-How to understand lazy function in Django utils functional module

32👍

This function takes function and any number of classes.
If to simplify, it returns wrapper(lets say “lazy function”) instead of that function. At that point we can say that we turned function
into lazy function.
After that we can call this lazy function. Once called, it will return instance of proxy class, without calling the initial
function instead of result of initial function.
The initial function will be called only after we invoke any method on that result(proxy instance).
*resultclasses here is the classes, instances of which are expected as results of the initial function

For example:

def func(text):
    return text.title()

lazy_func = lazy(func, str)
#lazy functon. prepared to dispatch any method of str instance.

res = lazy_func('test') #instance of __proxy__ class instead of 'Test' string.

res.find('T') #only at that point we call the initial function

I’ll try to explain how it works in overall:

def lazy(func, *resultclasses): #On decorate

    @total_ordering
    class __proxy__(Promise):
        __dispatch = None

        def __init__(self, args, kw): #On call
            #3) __proxy__ instance stores the original call's args and kwargs. args = ('Test', ) for our example
            self.__args = args
            self.__kw = kw
            if self.__dispatch is None:
                self.__prepare_class__()
            #4) if it's the first call ot lazy function, we should prepare __proxy__ class

            #On the first call of the __wrapper__ function we should prepare class. Class preparation in this case
            #means that we'll fill the __dispatch class attribute with links to all methods of each result class.
            #We need to prepare class only on first call.

        @classmethod
        def __prepare_class__(cls):
            cls.__dispatch = {}
            for resultclass in resultclasses:
                #5) Looping through the resultclasses. In our example it's only str
                cls.__dispatch[resultclass] = {}
                for type_ in reversed(resultclass.mro()):
                    #6) looping through each superclass of each resultclass in reversed direction.
                    # So that'll be (object, str) for our example
                    for (k, v) in type_.__dict__.items():
                        #7) Looping through each attribute of each superclass. For example k = 'find', v = str.find
                        meth = cls.__promise__(resultclass, k, v)
                        if hasattr(cls, k):
                            continue
                        setattr(cls, k, meth)
                        #9) If __proxy__ class doesn't have attribute 'find' for example, we set the __wrapper__ to
                        #that attribute
                        #So class __proxy__ will have the __wrapper__ method in  __proxy__.__dict__['find'].
                        #And so on for all methods.


        @classmethod
        def __promise__(cls, klass, funcname, method):
            # Builds a wrapper around some magic method and registers that
            # magic method for the given type and method name.
            def __wrapper__(self, *args, **kw): #При вызове каждого метода результирующего класса (str)
                # Automatically triggers the evaluation of a lazy value and
                # applies the given magic method of the result type.
                res = func(*self.__args, **self.__kw)
                #10 finally we call the original function
                for t in type(res).mro():
                    #11) We're looping through all the superclasses of result's class from the bottom to the top
                    #That''ll be (str, object) for our example
                    if t in self.__dispatch:
                        #12) If the class is dispatched we pass the result with args and kwargs to
                        #__proxy__.__dispatch[str]['find'] which is unbound method 'find' of str class
                        #For our example res = 'Test', args = ('T', )
                        return self.__dispatch[t][funcname](res, *args, **kw)
                raise TypeError("Lazy object returned unexpected type.")


            if klass not in cls.__dispatch:
                cls.__dispatch[klass] = {}
            cls.__dispatch[klass][funcname] = method
            #7) Adds __proxy__.__dispatch[str]['find'] = str.find for example which is unbound method 'find' of str class
            #and so on with each method of each superclass of each resultclass
            #8) Returns new __wrapper__ method for each method of each resultclass. This wrapper method has the
            #funcname variable in closure.

            return __wrapper__


    @wraps(func) #makes the lazy function look like the initial
    def __wrapper__(*args, **kw):
        # Creates the proxy object, instead of the actual value.
        return __proxy__(args, kw)
        #2)On call of lazy function we get  __proxy__ instance instead of the actual value


    return __wrapper__
    #1)As the result of lazy(func, *resultclasses) call we get the __wrapper__ function, which looks like
    #the initial function because of the @wraps decorator

Leave a comment