In the following program, will something be printed out, nothing, or an error… and why? →
s is a global variable. It is accessible everywhere, including the function body.
Variables Declared Inside a Function
In the following program, will something be printed out, nothing, or an error… and why? →
An error occurs because s is inaccessible outside of the function definition. s is local to the function that it was defined in.
Parameters
In the following program, will something be printed out, nothing, or an error… and why? →
An error occurs. You can't access the parameters (by their name) that you passed in to the function from outside of the function. Parameters are local to their function.
Precedence
In the following program, will something be printed out, nothing, or an error… and why? →
Variables created within a function are local to that function. A function will use a local variable before global. In this case, it will use the local variable, s, instead of the global variable, s.
A Quick Explanation
Scope
A scope:
determines where a variable is accessible
it holds the current set of all available names and the values that they refer to
Global Scope
anything that we define in the top level of indentation in our program is said to be in the global scope
in the following example, the variables a and b are in the global scope
they can be accessed from anywhere, even within the function
Local Scope
Variables that are defined in your function (one indentation level in), however, are only available within your function. They are local to that function. The example below won't work.
Local Scope Continued
Variables that are declared (created) within a function that have the same name as a global variable are totally different variables/values! They don't overwrite the outer, global variable (there's a way to do this, though). What will this print?
What is the exact error that you get if you try to use a variable, function or module that you haven't created yet? →
NameError (Let's try it →)
Creating Names
when you create a variable in a function, you're actually creating a name in the local scope
if there's a global variable that happens to be the same name, it is not affected!
Finding Names
If you use a variable name in a function, it will try to find that name in the following places in order:
local scope (variables defined in the function)
enclosing functions' locals (function can be defined within function definitions - we won't be using this, though)
global scope (variables defined in the top-level of your file)
built-ins (all of the built-in functions and variables that are available when Python starts)
And So?
What does the following code print out?
Scope Summary
global scope - variables and function definitions declared outside of a function; can be accessed everywhere - from current file or within function definition
local scope - variables declared inside of a function; cannot be accessed outside of the function they are created in
parameters are local to their function - parameters in a function cannot be accessed outside of a function either
local variables declared (created) with the same name as a global variable:
are accessed first if referenced in the function definition