Python Global Variables Notes

Objectives: Python Global Variables Notes

Python Global Variables Notes

Python Global Variables - Complete Notes

In Python, variables are storage containers for data. The location where a variable is created defines its scope. A global variable is a variable that is created outside of any function and can be accessed by any function or block in the module. This is different from local variables, which exist only inside the function where they are declared.

1. Global Variables

A global variable is defined outside any function. Once defined, it is accessible from both inside and outside of functions.

Example: Accessing a Global Variable inside a Function

x = "awesome"  # global variable

def myfunc():
    print("Python is " + x)  # accessing global variable inside function

myfunc()
# Output: Python is awesome

Explanation: The compiler parses the module top-down. When it reaches the function, it searches for x. Not finding a local x, it uses the global one.

Example: Local Variable with Same Name

x = "awesome"  # global variable

def myfunc():
    x = "fantastic"  # local variable
    print("Python is " + x)  # uses local variable

myfunc()
print("Python is " + x)  # uses global variable
# Output:
# Python is fantastic
# Python is awesome

Explanation: The function creates its own local variable x, which shadows the global variable. Outside the function, the global x remains unchanged.

2. The global Keyword

Normally, any variable assigned inside a function is local. To modify or create a global variable inside a function, we use the global keyword.

Example: Creating a Global Variable inside a Function

def myfunc():
    global x  # declare x as global
    x = "fantastic"

myfunc()
print("Python is " + x)
# Output: Python is fantastic

Explanation: Here, the compiler treats x as a global variable when encountered inside myfunc(). Python updates the module-level namespace.

Example: Modifying an Existing Global Variable

x = "awesome"  # global variable

def myfunc():
    global x
    x = "fantastic"  # modifies the global variable

myfunc()
print("Python is " + x)
# Output: Python is fantastic

Explanation: Without global, assigning x inside the function would create a local variable, leaving the global variable unchanged.

3. Key Points About Global Variables

  • Global variables exist for the lifetime of the program.
  • All functions in the module can read a global variable by default.
  • To modify a global variable inside a function, use global.
  • Using the same variable name locally does not change the global variable.
  • Global variables are stored in the module-level namespace, which the Python interpreter manages internally.

4. Real-life Analogy

Think of a global variable as a public bulletin board in a company. Everyone can read what is posted on it. If someone adds or updates a notice using the global keyword, it becomes visible to everyone. If an employee writes a note privately (local variable), it is only visible on their own desk (function).

5. Compiler Behavior Explained

When Python runs a program:

  1. The Python compiler first parses the code and creates a symbol table for each scope (global and local).
  2. When it encounters a variable inside a function, it checks the local scope first.
  3. If it sees global, it binds the variable to the module-level scope.
  4. Reading a variable without assignment automatically uses the global version if no local version exists.
  5. Assignment without global creates a new local variable.

6. Exercise Example

x = 'awesome'

def myfunc():
    x = 'fantastic'  # local variable, does not affect global x

myfunc()
print('Python is ' + x)
# What will be printed?
# Answer: Python is awesome

7. Summary

- Global variables are created outside functions.
- Local variables exist only inside the function.
- Use global keyword to create or modify global variables inside a function.
- Python's compiler manages scope using namespaces and symbol tables.
- Shadowing a global variable with a local one does not affect the global variable.

8. Advanced Tip

Be careful when using too many global variables; it can make debugging harder. Use functions with parameters and return values where possible to maintain clean and maintainable code.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::