Page 141 -
P. 141

local garbage removal


           When you leave a function, its

           variables get thrown away


           Each time you call a function, Python creates a new stack frame to
           record new variables. But what happens when the function ends?


           The computer throws away the                                             File this under G, for “garbage."
           function’s stack frame!

































           Remember: the stack frame is there to
           record local variables that belong to the
           function. They are not designed to be used elsewhere
           in the program, because they are local to the function. The whole reason
           for using a stack of variables is to allow a function to create local   When a variable's
           variables that are invisible to the rest of the program.            value CANNOT be

           And that’s what’s happened with the password variable. The first
           time Python saw it was when it was created in the set_password()    seen by some code,
           function. That meant the password variable was created on the
           set_password() function’s stack frame. When the function ended,     it is said to be “out
           the stack frame was thrown away and Python completely forgot about
           the password variable. When your code then tries later to use the   of scope.”
           password variable to access Twitter, you’re outta luck, because it can’t
           be found anymore...

           106    Chapter 3
   136   137   138   139   140   141   142   143   144   145   146