I want to understand the steps Python is taking to go through the nested function below:
def raise_val(n):
def inner(x):
raised = x ** n
return raised
return inner
cube = raise_val(3)
print(cube(5))
125
I am trying to wrap my head around how this works as I cannot seem to figure it out.
Why doesn't Python give any value if you just do print(raise_val(any #)), and how does setting cube = raise_val(any #) make cube a function? I don't understand how when you initially put a number in raise_val, such as raise_val(3), it makes n = 3 within the inner(x) function, but can still return a value to store even when x is still empty. When you do cube = raise_val(3), and then follow it with cube(5), how does that make x = 5 within the inner(x) function, so that the variable raised = 5 ** 3? How does it know which variable to fill with which number input?
I am confused but I truly want to understand as I feel like this is something I need to know in order to be proficient in Python. This is my first question ever posted so I hope I explained it clearly. Any insight would be greatly appreciated, thank you!
I tried playing around with the code, trying different inputs, but still cannot understand the process. I can memorize it, but I want to be able to do more than that!