Hello dear DQ community,
I’m currently in the middle of Building a Pipeline Class in the Data Engineering path and I must admit I’m a bit struggling with the partial module and function closures.
So for instance in
The Code is :
def add(A): def inner(b): # Use `A` from the argument in # the parent add() function. return A + b return inner add_two = add(2) print(add_two(7))
My understading is the following
add_two is a variable that stores the returned value of the
add function accepts one parameter, in this case
The point where it gets troubling for is:
- where else is
add_twodefined so that it can accept a parameter too?
Because clearly the inner function, aptly even though generecally called inner, accepts the parameter
band from the example it is provided by the
As I do everytime I get stuck, I tried tweaking the code snippet to better grasp the inner working.
print(add_two) outputs the memory location of the variable so it’s of no use
print(add_two()) outputs the traceback
TypeError: inner() missing 1 required positional argument: 'b'
So this confirms that
add_two are linked.
The following paragraph in the lesson supposed to explain it confuses me even more
Finally, notice that when we call the
add()function with 2, the
inner()function is returned to the
add_twovariable with the saved parent variable, A=2. That is, when we call the
add_two()function, we are actually calling the
inner()function with saved parent variables. It means that when the
add()function is called, the
inner()function is saved in working memory, ready to be called with those default values.
I think it means that the first line with
add_two refers to
add_two as a variable and the second line with
add_two(7) refers to
add_two as a function?
But when is
add_twoas a function defined??
All my confusion is maybe due to my poor understanding of the partial module that was first mentionned in the introductory course of this part 7, functionnal programming Learn data science with Python and R projects.
partialmodule takes in a function, and “freezes” any number of args (or kwargs), starting from the first argument, then returns a new function with the default inputs.
I don’t think I understand what " returns a new function with the default inputs " actually means .
What are these " default inputs " mentionned? When are they even defined?
Could someone please help me understand how this works?
Thank you very much in advance, it’s greatly appreciated.