Advantage of Python 3 (Functions and Variables)
Assigning functions to variables is a valuable feature in functional programming (example: when using closures) just like polymorphism is an extemely powerful feature in OOP.
Though this analogy, might seem to be peculiar at first but existence of both these features in the respective programming paradigms gives a programmer some control over runtime flexibility.
I’ll try to explain the reason for using variables to refer functions by first explaning what advantages polymorphism offers in OOP (as its a familiar concept, so it will be easy to relate)
Polymorphism (In context of functions) allows you to use a same function name to define different versions of the function that takes different types of inputs, and is called upon different types of objects. So, depending upon the types of inputs or the type of object (eg: whether it’s parent object that invoked the function or child object) that suitable version of the function will be called and exceuted. So, context is used to determine which version of the function to be called (Programmer just has to use that same name and the underlying detection of context is supported by polymorphism feature). This is a classic example of runtime flexibility.
Now, consider a scenario where you have a function A that generates a function B based on some input such that for every different input of function A, a new function will be generated. Now ask yourself, if you don’t assign these customized functions (function B) which are returned by function A at runtime to a variable, how will you refer these functions(function B). That is one among many, but an important reason to assign functions to variables.
FYI, the functions returned by another functions which refer to some value in the outer function in which they are defined, are termed as closures.
In the below example, multipleOf is a closure, and multiple is the outer function.
return num%n == 0
Consider the above example, there is
multiple function that takes any number n and returns a function
multipleOf that will check the divisibility of any number num by n. So, in a way,
multipleOf function is a customized one depending upon the value of n given to
multiple at runtime.
multiple(3) will generate
multipleOf function that will check any number’s divsibility with 3, Calling
multiple(5) will give a function that checks divisibility of a number for 5. If you don’t assign these different versions of
multipleOf function to a variable, how will you be able to use it?
This is a trivial example, ofcourse, because you can create a generic function that takes both the dividend and divisor as the input.
But, sometimes, when solving a complex problem, there will be situations when you don’t have all the inputs required to compute a result, then you can create a function that takes the input parameters which are currently known, create a closure with input parameters that are yet to be known inside this outer function and use the input values of the outer function to define it and defer the computation of the result to the closure returned by this outer function.
Now, when your program will have the rest of the inputs available to compute the result, you can invoke the closure earlier assigned to some variable with now available input parameters.
Also Read: Review of Learn Python The Hard Way