In Python, variables are classified into global and local based on where they are declared and their scope (the part of the program where the variable can be accessed). Understanding this distinction is crucial for writing efficient and error-free code.
1. Local Variables
- Definition: Variables declared inside a function are called local variables. Their scope is limited to the function in which they are defined.
- Usage: Local variables are created when the function is called and are destroyed when the function ends.
Example:
def my_function():
x = 10 # Local variable
print("Inside the function, x =", x)
my_function()
# print(x) # Error: x is not accessible outside the function
2. Global Variables
- Definition: Variables declared outside of any function are called global variables. They can be accessed and modified from any part of the program.
- Usage: These variables are created when the program starts and persist until the program ends.
Example:
x = 20 # Global variable
def my_function():
print("Inside the function, x =", x)
my_function()
print("Outside the function, x =", x)
3. Modifying Global Variables Inside a Function
- By default, Python considers a variable local if it is assigned a value inside a function. To modify a global variable inside a function, you must use the
global
keyword.
Example:
x = 30 # Global variable
def modify_global():
global x
x = 50 # Modify global variable
print("Inside the function, x =", x)
modify_global()
print("Outside the function, x =", x)
Output:
Inside the function, x = 50
Outside the function, x = 50
4. Local vs. Global Variables in the Same Program
If a variable with the same name exists as both a local and global variable, the function will prioritize the local variable.
Example:
x = 100 # Global variable
def my_function():
x = 200 # Local variable
print("Inside the function, x =", x)
my_function()
print("Outside the function, x =", x)
Output:
Inside the function, x = 200
Outside the function, x = 100
5. Nonlocal Variables (In Nested Functions)
If a variable is neither local nor global but exists in an enclosing function, it can be accessed using the nonlocal
keyword in a nested function.
Example:
def outer_function():
x = 10 # Enclosing variable
def inner_function():
nonlocal x
x = 20 # Modify enclosing variable
print("Inside inner_function, x =", x)
inner_function()
print("Inside outer_function, x =", x)
outer_function()
Output:
Inside inner_function, x = 20
Inside outer_function, x = 20
Key Differences Between Global and Local Variables
Feature | Local Variable | Global Variable |
---|---|---|
Scope | Inside the function where declared | Entire program |
Lifetime | Exists during function execution | Exists for the entire program |
Declaration | Inside a function | Outside any function |
Keyword | No keyword required | Use global inside functions to modify |
Best Practices
- Use Local Variables: Prefer local variables to avoid unintended modifications and make your code easier to debug.
- Minimize Global Variables: Use global variables sparingly, as they can make your program harder to understand and debug.
- Use
nonlocal
When Necessary: Usenonlocal
in nested functions only when modifying enclosing variables is absolutely required.
Let me know if you’d like more examples or clarification!