How does variable scopes work in Python Modules?
Answers
variables and scope in the online Python textbook. The scope of a variable refers to the places that you can see or access a variable. The variable a is therefore said to be local to the function. Put another way, the variable a has local scope.
Recall that a variable is a label for a location in memory. It can be used to hold a value. In statically typed languages, variables have predetermined types, and a variable can only be used to hold values of that type. In Python, we may reuse the same variable to store values of any type.
A variable is similar to the memory functionality found in most calculators, in that it holds one value which can be retrieved many times, and that storing a new value erases the old. A variable differs from a calculator’s memory in that one can have many variables storing different values, and that each variable is referred to by name.
Defining variables
To define a new variable in Python, we simply assign a value to a label. For example, this is how we create a variable called count, which contains an integer value of zero:
count = 0
This is exactly the same syntax as assigning a new value to an existing variable called count. Later in this chapter we will discuss under what circumstances this statement will cause a new variable to be created.
If we try to access the value of a variable which hasn’t been defined anywhere yet, the interpreter will exit with a name error.
We can define several variables in one line, but this is usually considered bad style:
# Define three variables at once:
count, result, total = 0, 0, 0
# This is equivalent to:
count = 0
result = 0
total = 0
In keeping with good programming style, we should make use of meaningful names for variables.
Variable scope and lifetime
Not all variables are accessible from all parts of our program, and not all variables exist for the same amount of time. Where a variable is accessible and how long it exists depend on how it is defined. We call the part of a program where a variable is accessible its scope, and the duration for which the variable exists its lifetime.
A variable which is defined in the main body of a file is called a global variable. It will be visible throughout the file, and also inside any file which imports that file. Global variables can have unintended consequences because of their wide-ranging effects – that is why we should almost never use them. Only objects which are intended to be used globally, like functions and classes, should be put in the global namespace.
A variable which is defined inside a function is local to that function. It is accessible from the point at which it is defined until the end of the function, and exists for as long as the function is executing. The parameter names in the function definition behave like local variables, but they contain the values that we pass into the function when we call it. When we use the assignment operator (=) inside a function, its default behaviour is to create a new local variable – unless a variable with the same name is already defined in the local scope.
Here is an example of variables in different scopes:
# This is a global variable
a = 0
if a == 0:
# This is still a global variable
b = 1
def my_function(c):
# this is a local variable
d = 3
print(c)
print(d)
# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)
# a and b still exist
print(a)
print(b)
# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
Note
The inside of a class body is also a new local variable scope. Variables which are defined in the class body (but outside any class method) are called class attributes. They can be referenced by their bare names within the same scope, but they can also be accessed from outside this scope if we use the attribute access operator (.) on a class or an instance (an object which uses that class as its type). An attribute can also be set explicitly on an instance or class from inside a method. Attributes set on instances are called instance attributes. Class attributes are shared between all instances of a class, but each instance has its own separate instance attributes. We will look at this in greater detail in the chapter about classes.
The assignment operator
As we saw in the previous sections, the assignment operator in Python is a single equals sign (=). This operator assigns the value on the right hand side to the variable on the left hand side, sometimes creating the variable first. If the right hand side is an expression (such as an arithmetic expression), it will be evaluated before the assignment occurs