0
def raise_val(n):
    def inner(x):
        raised = x**n
        return raised
    return inner

square = raise_val(2)
print(square(2))# gives 4 as output

I don't understand how this nested function works...also why does square(2) output 4?
Isn't square just a variable?

martineau
  • 119,623
  • 25
  • 170
  • 301
user222991
  • 25
  • 4
  • 3
    What do you mean "provide" it? And *"I don't understand"* isn't actually a question. – jonrsharpe Mar 24 '19 at 22:22
  • you might want to look in to first class objects and how they work in python – gold_cy Mar 24 '19 at 22:23
  • gives 4 as output. – user222991 Mar 24 '19 at 22:25
  • Yes, you said that in the question. It's not clear why that was unexpected or what you're trying to ask. Square is just a variable, yes, but its value is a function. – jonrsharpe Mar 24 '19 at 22:27
  • 1
    `type(square)` would be enlightening. – chepner Mar 24 '19 at 22:36
  • ivan_pozdeev: plz elaborate...I am new to programming...the link is hard for me to understand... – user222991 Mar 24 '19 at 22:37
  • what is the value of x in this operation? we only feed the value of n inside...how can n be equal to 2...I don't understand how type(square) is a function...shouldn't it be an integer? – user222991 Mar 24 '19 at 22:39
  • If you are really new. I'd suggest coming back to this after you have some more of the basics under your belt. It's not super advanced in the scheme of things, but sometimes when there are just too many new concepts involved it can be tough going to figure out. – Paul Rooney Mar 24 '19 at 22:40
  • Why would square be an integer? raise_val returns inner, which isn't an integer. – jonrsharpe Mar 24 '19 at 22:46
  • https://stackoverflow.com/questions/27423550/understanding-function-closures?noredirect=1&lq=1. This link solved my problem...thanx all – user222991 Mar 24 '19 at 22:58

4 Answers4

2

This is called higher-order functions in functional programming languages. Python also uses this paradigm.

A higher-order function is a function that either/both accepts a function as an argument and/or returns a function as a result. Your function raise_val is a higher order function in the sense that it is returning back a function. Your variable square has a function (the inner).

When you call raise_val(2) what you do is that you return a partial function that needs to be evaluated. Then with square(2) you are passing 2 to the inner partial function that uses the 2 for n when you initially called raise_val(2).

Rafael
  • 7,002
  • 5
  • 43
  • 52
1

In python, functions are values, too.

So, every time that you call raise_val, you are generating a new "inner" function which has a different constant for the n variable.

You are assigning this new function to the variable square, and then calling this new function.

martineau
  • 119,623
  • 25
  • 170
  • 301
LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

When you type square = raise_val(2), you provide the n=2 parameter to inner and raise_val returns you the inner function and stores it in thesquare variable.

def raise_val(n):
    def inner(x):
        raised = x**n
        return raised
    return inner # This returns a function, not a number!

This is why you can call square later by writing square(2). Here you provided the x=2 argument.

print(square(2))# gives 4 as output

What you did is equivalent to printing raise_val(n=2)(x=2).

Guimoute
  • 4,407
  • 3
  • 12
  • 28
0

Line by line explanation:

  • def raise_val(n): begin defenition of a function, raise_val, which accepts n as a parameter.

    • def inner(x): begin defenition of a function within raise_val, called inner, which accepts x as a parameter
      • raised = x**n set variable raised as x(the argument given to inner) to the power of n(the argument given to raise_val)
      • return raised return raised as the return value of inner
    • return inner return the function inner as the return value of raise_val
  • square = raise_val(2) set a new variable square as the output of the function raise_val when called with an argument 2, which will be a function that takes a number x and return it squared

  • print(square(2)) call the variable square with paramter 2, thus returning 2 to the power 2, or 4, which it then prints
Artemis
  • 2,553
  • 7
  • 21
  • 36