Nearby lessons
118 of 159Python - Scope
- Understand what scope means in Python
- Know the relationship between functions, modules, and libraries
- Understand global variables and where they can be accessed
- Understand local variables and where they can be accessed
- Compare global and local variables
What is Scope?
The scope of a variable is the part of a program where that variable can be accessed.
Every variable in Python is created in a particular scope. Some variables are available throughout the module, while others are available only inside a single function.
Understanding scope is important because it decides exactly where a name is visible in our code.
Function vs Module vs Library
Before discussing variables, it is helpful to understand the relationship between these three terms.
| Term | Meaning |
|---|---|
| Function | A group of lines with a name. |
| Module | A group of functions saved to a file. |
| Library | A group of modules. |
Hierarchy of Functions, Modules, and Libraries
The relationship can be understood with the following hierarchy:
Library
├── Module 1
│ ├── Function 1
│ ├── Function 2
│ └── Function 3
└── Module 2
├── Function 1
├── Function 2
└── Function 3Types of Variables
Python supports 2 types of variables based on scope:
- Global Variables
- Local Variables
Global Variables
The variables which are declared outside of a function are called global variables.
These variables can be accessed in all functions of that module.
Explanation
The variable a is declared at module level, outside any function, so it is a global variable.
Both f1() and f2() can access a, because global variables are visible inside every function of the module.
Local Variables
The variables which are declared inside a function are called local variables.
Local variables are available only for the function in which we declared them. From outside the function, they cannot be accessed.
Explanation
The variable a is declared inside f1(), so it is a local variable of f1().
Inside f1(), printing a works normally. But inside f2(), the name a is not in scope, so Python raises a NameError.
Global vs Local Variables
| Global Variables | Local Variables |
|---|---|
| Declared outside a function. | Declared inside a function. |
| Accessible in all functions of the module. | Accessible only inside the function where they are declared. |
| Created when the module is loaded. | Created when the function is called. |
| Exists for the lifetime of the module. | Exists only until the function returns. |
Why Scope Matters
- It prevents variables from one function accidentally changing variables in another function.
- It allows the same variable name to be reused safely in different functions.
- It keeps the module organized because the visibility of every name is clear.
- It avoids name conflicts between different parts of a program.
- Scope decides which parts of a program can access a given variable
- Global variables are declared outside functions and are accessible in every function of that module
- Local variables are declared inside a function and are accessible only within that function
- A local variable cannot be accessed from outside the function where it is declared
- Functions combine to form a module, and modules combine to form a library