I'm up to Exercise 41 in Learn Python the Hard Way, and I'm having a really hard time wrapping my brain around the fact that the entire thing hinges on a function running just because it's been assigned as a value to a variable. I wrote up 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 being:
Put on some pants!
And don't forget your underwear!
So obviously this happens, but I can't understand why the language works that way -- what the logic is behind the language that makes this a valuable way of operating. I think it'd be helpful for me moving forward to understand why this is, rather than just "that's the way it works."
For clarity: I'm asking (I guess) why the function is running, when all I'm doing is assigning it as a value for something. The print statements are just there so I can see that the function is indeed running.
It's the fact that I'm not ever actually running
pants() shorts()
that is confusing me.
To create a tortured analogy, if me-baking-cookies-at-home were "cookies()", and I were to make cookies on Saturdays, I might eventually believe that
Saturday = cookies()
but just thinking "hey, Saturday is cookie day" is not the same as actually baking cookies... so why does just saying
Saturday = cookies()
actually bake the cookies, rather than just setting up Saturday with the variable "cookies()" for some later use?
print
withreturn
. – JJJ Mar 11 '12 at 14:24zap
to have a value of "Put on some pants!"? If so then change theprint
to areturn
and thenprint zap
after it's been assigned. – chooban Mar 11 '12 at 14:27