Function returning another function

Asked

Viewed 137 times

-2

How the second function is returning the function f?

And where the function is being returned to f since, when I give print in the variable, the value returned is 'hi' and 'None'?

def f(var):
    print(var)
    
def funcao():
    return f

variavel = funcao()('OI')

Return:

OI
None

função no python retornando outra função

  • I wanted to know where is being returned the function f and why put the second parenthesis with value ('hi') because if I give a print on the variable the value will be None!

  • 2

    If it were x = funcao(), what would be the value of x?

  • Unintentionally, it almost manages to implement a more sophisticated language device where many people have difficulty understanding how it works.

  • The value of x would be None

2 answers

3

Okay, confusion is with unified syntax. A lot of people don’t really understand the syntax of the language, whatever it is. It is important to understand everything that is happening and to know what each character does in the code, what is happening there, to understand the concepts, and all interconnections of each existing mechanism in the language. I say this in my lectures and even put here, but people do not realize how important it is to program well:

Enquanto você não souber o que cada caractere do seu código faz, até mesmo o espaço em branco, você ainda não sabe programar.

You know What is a variable?

You know What is a literal?

So when funcao() is called it returns the value contained in the identifier called f. It turns out that this identified has as value a function, described just above. So it’s like a variable (technically it’s a constant, even though Python doesn’t have this concept for other forms). When the return is made you can use this value somewhere. One of the things you can do is store it in a variable. But you don’t have to do that, you can use it right there in the expression you’re using, you use it in a way inline.

So let’s think of a slightly different code:

def funcao():
    return 1
variavel = funcao() + 2
print variavel

Can you tell what’s going on there? After the return of the value it’s like the code is like this:

variavel = 1 + 2
print variavel

It’s pure mathematics, we replaced something we previously didn’t know about the value, so it was a variable or constant, with a known value, and as we do that we get a result. In the case funcao() is what we didn’t know the value and which was replaced by something known after the function was called by returning the value.

You can understand that?

def f(var):
    print(var)
def funcao():
    return f
variavel = funcao()
variavel('OI')

What is being stored in variavel? It’s a function called f, right? So now variable `has as its value a function that I can call it.

Then making the replacement of the function by its return would be so:

def f(var):
    print(var)
variavel = f
variavel('OI')

And to call a function always use the parentheses right after the identifier that names the function, can pass arguments or not, depending on the function. In this case the function I have is stored in the variable with the identifier variavel and so soon after called this function with this identifier passing an argument within the parentheses.

Now, because I need to save something in a variable that I just want the result. If I didn’t know, now reading the link above you should know that a variable is a name for a storage location, and in your code you do not need to store anything,, just take the value and use.

Then we return to the initial example of the answer where you receive a value returned by the function and already use immediately. Instead of using the addition operator + as I used there, is now using function call operator (), or as was used now passing argument ('OI'), so the literal 'OI' It’s like I’m the 2 of my first example.

Note that there is nothing special about the concept. It just had an intersection of different mechanisms. Probably already knew how to call a function and use in an expression. Probably already knew uses ruma function anonymous, but for some reason thought that could not use the two together equal does with a numerical value or otherwise. It is a linear question, if thinking well it would not even make sense to be different, if it works one way why would it prevent it from working another? There may be some reason, but in this case there is.

So replacing the function with its return in your code would be:

def f(var):
    print(var)
variavel = f('OI')

I put in the Github for future reference.

in that case nor does it make sense to have the variable variavel because it stores a null value since it even has a return.

  • I can use that image?

  • 1

    Can, of course....

  • I did not understand the part of the "variable = f" and "variable('hi')". Will after I return the function f ,the variable pass to be a function and after I call the variable and pass a value "hi", for example, this value goes to the function f I set up there and shows the value on the screen?

  • Exactly that.

  • And how this without variable works "function()('Hi')" ?

  • Is explained in the reply.

  • Man, I think I got it! when the variable receives the return of the function f, the variable becomes a function, i.e., "variable = function()" is the same thing as "variable = f", then variable=f is equivalent to a function only, which I can call it in this way " variable('Hi')", or so: variable = function()('HI'). Is my thinking right?

  • Yes, exact.....

  • Thank you friend! you are very good.

  • @Felipesoares take a look at [tour] how to accept an answer if it solved your problem.

  • I already did. TMJ!

  • Did not accept, when do will appear the green light just below the votes. , there on the left of the answer.

Show 7 more comments

2

someone could explain to me how the second function is returning to function f?

The function funcao is returning f, which is a function. When you do:

variavel = funcao()("OI")
print(variavel)

is the same thing as:

retorno = funcao()
variavel = retorno("OI")

print(variavel)

[...] and where the function f is being returned to since when I print the variable the value returned is 'hi' and 'None'.

The function f does not return anything. You are just using print within it. For this reason it returns None.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.