Why does foo = function () run a function in Python?

I'm working on Exercise 41 in the "Learn Python the Hard Way" section, and it’s really hard for me to wrap my brain around the fact that it all depends on a function that only performs because it was assigned as a value for the Variable. I wrote a little script to confirm that this is how it works, and it does:

def pants(): print "Put on some pants!" def shorts(): print "And don't forget your underwear!" zap = pants() thing = shorts() 

With the results:

 Put on some pants! And don't forget your underwear! 

So it’s obvious that this is happening, but I can’t understand why the language works that way - that logic is behind the language, which makes it a valuable way of working. I think it would be useful for me to move on to understand why this is so, and not just "the way it works."

For clarity: I ask (I think) why a function works when everything I do assigns it as a value for something. The print statements are there, so I see that the function really works.

It is a fact that I have never run

pants () shorts ()

what scares me.

To create a painful analogy, if me-baking-cookies-at-home had "cookies ()" and I had to make cookies on Saturdays, I could eventually believe that

Saturday = cookies ()

but just thinking “hey Saturday is a cookie day” is not the same as actually baking cookies ... so why just says

Saturday = cookies ()

actually bake cookies, rather than just setting Saturday with the variable "cookies ()" for some later use?

+7
source share
7 answers

When you use parentheses () , the function is called. If you want to assign a function to a variable for reuse, you must remove the parentheses there.

Example:

 def pants(): print "Put on some pants!" def shorts(): print "And don't forget your underwear!" zap = pants thing = shorts 

And then when you want to call these functions:

 zap() thing() 
+16
source

So it’s obvious that this is happening, but I can’t understand why the language works that way - that logic is behind the language, which makes it a valuable way of working. I think it would be useful for me to move on to understand why this is so, and not just "the way it works."

The language must somehow distinguish between a function and the action of a function call. This is what parentheses provide.

 f = foo 

Now f attached to the function itself. The foo function can be executed by f() .

 f = foo() 

This calls the foo function and binds the return value to f .

Note that binding the return value to the name does not matter. Just write

 foo() 

will also execute the function, but the return value will simply be ignored.

+6
source

Although it may seem that your functions do not return anything, they are in fact. Quoting the Python.org documentation :

The returned statement returns the value from the function. return without expression argument returns None. Falling the end of a function also returns None.

So your functions really look like this:

 def pants(): print "Put on some pants!" return None def shorts(): print "And don't forget your underwear!" return None 

Assignments are assigned to zap regardless of what pants returns (i.e. the value of pants() ), and to thing regardless of shorts . In your case, both values ​​are None , but, of course, the functions must be executed in order to understand this (*). In the end, it may be that pants returns 42 for leap years and that shorts returns 'Foobar' whenever some random number generator “flips” 6.


(*) Derogation: functions to be performed should not be considered universal. In a clean setup and leaving aside the specifics of Python (which I know very little about), the compiler can understand that both functions are identical to None and do not cause any calls when the program starts. But a function that prints something (or checks if the current year is a leap year or rolls a cube) will not be clean.

+5
source

zap = pants() will bind the return value of the function to a variable, so, of course, the function runs if you bind it to a variable.

 def foo(): return 1 var = foo() print var 

prints 1 .

Hope this helps.

Edit: if you expect the value of the variable to be “superimposed on some pants”, you really confuse print and return , as people pointed out in the comments.

+2
source

When the interpreter sees the name of the function, followed by (), it knows that it must perform this function.

What you do there says: "Assign the result of these functions to these variables."

But since you are not returning any values ​​from these functions, you do not see anything in the variables.

However, since you have a print statement, you see that the interpreter performs these functions when it tries to assign the variable the results of this function.

+1
source

Your functions are called because of the brackets after the name:

 zap = pants() 

This calls the pants function and puts the result in zap. If you did this instead:

 zap = pants 

then zap will now reference the pants function.

And if you just wrote

 pants() 

then the pants function will also be called, but the result (which is None) would never be placed in a variable.

0
source

From your question, what I think you want the zap will have the value "Put on some pants!" , and thing will mean "And don't forget your underwear!" . If this is your problem, discuss it. Otherwise, you do not need to read further, as I just discussed about all this.

Let it be fun. When you define a function, you are like creating a machine that does what you want. Now think about the car, that when you give her some food, she cuts them off and ... does nothing! I mean, I made this machine to cut food and nothing more! You will not get your chopped food, but in fact it chopped your food, as you did.

Now that you want your chopped food back, you create another machine that takes your food, beats it and returns them to you. A fruitful machine, right ?; -)

They are all true for programming or math functions (although I don't know any void function in math !: P). When you create a function, you must tell it whether it simply does some work or does some work and returns a result. A way to specify a function is with the return . In your functions, you just said do something. And this print "......" , as well as functions. You call the with () command at the end and do your job, they print it. But, as I said, if you do not tell him to return the result, this will not happen. And since it does not return any result, nothing will be assigned to the variable (do not confuse it with None ). When you write these lines (if in the interpreter) or run the script, you will see that these lines are printed, but your zap and thing have no meaning.

So how to fix this? Tell them to return rows to variables. To report this, replace the print statements with return . And it doesn’t matter to experiment that you know, to know about what you know about your knowledge, right :-)

Hope this helps :-)

0
source

All Articles