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
Screen Link:
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.
The add
function accepts one parameter, in this case A
.
The point where it gets troubling for is:
- where else is
add_two
defined so that it can accept a parameter too?
Because clearly the inner function, aptly even though generecally called inner, accepts the parameterb
and from the example it is provided by theadd_two
variable/function.
As I do everytime I get stuck, I tried tweaking the code snippet to better grasp the inner working.
But print(add_two)
outputs the memory location of the variable so it’s of no use
And print(add_two())
outputs the traceback TypeError: inner() missing 1 required positional argument: 'b'
So this confirms that inner
and 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, theinner()
function is returned to theadd_two
variable with the saved parent variable, A=2. That is, when we call theadd_two()
function, we are actually calling theinner()
function with saved parent variables. It means that when theadd()
function is called, theinner()
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_two
as 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.
The
partial
module 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.