Scope and Lifespan of Identifiers
Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s variables and function names) belongs to that function The identifier is accessible (useable) in that function and nowhere else, it is local to the function This is the default behavior of variables (not just in Python) It is a GOOD thing because it allows you to focus on solving the problem that the function is supposed to solve, without worrying about what other functions used for names for their variables
Global variables The other scope that exists in a program file is “global”. This is an identifier which is defined outside of ALL function definitions An identifier which is global can be accessed anywhere in the file, in any function which appears below the definition of the identifier Using Global variables is considered NOT good style – and are not allowed in this class Some people want to do it because they think it makes functions easier to write – no parameter lists! Everything is global! If parameter lists are very difficult for you, then you need to understand your function’s purpose. If you cannot decide what needs to be a parameter, ask yourself “will the function get this info from outside, or will it generate it itself?”
Why global variables are bad style If you want to copy a function to use in another program, and the function uses a global variable, you must also copy the global variable definition If you are working as part of a team on a program, if you create a global variable, there is NO WAY to prevent other team members from using/changing your global – you have no way to control access If you want a function to be general, you do NOT use global variables – it is much more difficult to keep changing the global variable between calls than it is to call the function with different arguments They make testing and debugging much harder!
Lifespan of a variable How long does a variable “live”? That is, when during the execution does a variable have memory and retain its value? This is a dynamic way of thinking about variables, as opposed to scope, which is more static If you had to describe the scope of a variable, you could print out the code of a program and circle the scope on the paper – it does not change If you had to describe the lifespan of a variable, you would have to describe when the variable was created and when it was destroyed, which varies depending on the execution of the program
Lifespans The lifespan of a variable local to a function is from the time it is created in the function’s run, until the function ends (and the memory allocated to the variable is released) It’s the same for parameters to that function also. The lifespan of a global variable is from the time its definition is encountered during the execution of the whole program, until the program is finished If a function with locals is called several times, then those variables are created and destroyed, created and destroyed, over and over for each call, their lifespan is intermittent