2

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!

K-888
  • 21
  • 3
  • 1
    [This question and its answers](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work/111111#111111) are about JS, but almost everything applies to Python as well. Could you read through that and then comment if you have any unresolved questions? – Zac Anger Dec 08 '22 at 02:56
  • 1
    Think of `raise_val()` as a _function factory_. It makes functions. When you say `raise_val(5)`, you're asking it "make me a function that accepts an argument and raises it to the power 5". – John Gordon Dec 08 '22 at 03:15
  • 1
    `cube` and `inner` are two names for the same object, a function. That function takes a parameter (`x`), and also has access to a *nonlocal* variable (`n`) from the function containing its definition. – jasonharper Dec 08 '22 at 03:21
  • Thanks so much for these comments! I really appreciate it. I just spent hours going through the article, playing around with the code while nesting additional functions within, and re-reading all the comments again. My head hurts, but I feel like at least I'm grasping the general concept now, but probably need more practice in actually using these nested functions, as I haven't gotten around to seeing why this is needed in a practical setting yet. Seeing the code as raise_val(3)(5) being the same as cube(5) helped a bit as well. This was the trickiest concept for me so far, so thanks again! – K-888 Dec 08 '22 at 17:02

1 Answers1

0

First, I think another way to write this function is by using one function with two parameters (n and x). Another error is you are naming the value of "raising" when you call the raise_val function, which should be an integer. Therefore, you can't use it as a function again like cube(5). Here is my code. I hope this help!

def raise_val(n,x):
   return x**n

print(raise_val(3,5))

alexz0
  • 1
  • Thanks for your contribution! Sorry, I may not have been clear on my question, as I was asking how a nested function worked and how the variables were being read by Python since I was having trouble understanding it. Thanks again for taking your time to answer, I appreciate it! Next time I will make my question more clear. – K-888 Dec 08 '22 at 16:00
  • Does not answer OP's question – roeen30 Dec 11 '22 at 15:38