SyntaxError: name 'X' is used prior to global declaration

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# SyntaxError: name 'X' is used prior to global declaration

The Python "SyntaxError: name 'X' is used prior to global declaration" occurs when we reference a variable in a function prior to marking it as global.

To solve the error, move the global declaration at the beginning of your function's definition.

syntaxerror name is used prior to global declaration

Here is an example of how the error occurs.

main.py
employees = ['Alice', 'Bob'] def my_func(): # โ›”๏ธ SyntaxError: name 'employees' is used prior to global declaration print(employees) global employees employees = ['Carl', 'Dean'] my_func()

accessing before global declaration

The error is caused when we reference the variable before marking it as global.

# Move the global statement to the top of the function definition

To solve the error, move the global your_variable statement to the top of the function's definition before any references to the variable.

main.py
employees = ['Alice', 'Bob'] def my_func(): # ๐Ÿ‘‡๏ธ Is now moved to the top global employees print(employees) employees = ['Carl', 'Dean'] my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob'] my_func() # ๐Ÿ‘‰๏ธ ['Carl', 'Dean']

moved global statement to top

We moved the global statement to the top of the function which resolved the error.

We aren't allowed to access a variable that is marked as global in the same code block preceding the global statement.

Once the global statement runs, we can access the variable within the function, update the variable's value or even delete it.

# You don't have to use global to access a global variable

If you only have to read the global variable within your function, you don't have to use the global statement.

main.py
employees = ['Alice', 'Bob'] def my_func(): print(employees) my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob']

You can even modify the global variable from within the function, even though this isn't considered a good practice.

main.py
employees = ['Alice', 'Bob'] def my_func(): employees.append('Carl') print(employees) my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob', 'Carl'] my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob', 'Carl', 'Carl'] my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob', 'Carl', 'Carl', 'Carl']

modifying global variable from within function

We are able to use the list.append() method to modify the global employees variable.

# You have to use global to reassign a global variable

However, if you need to reassign the global variable from within the function, you have to mark the variable as global.

main.py
employees = ['Alice', 'Bob'] def my_func(): global employees print(employees) employees = ['Carl', 'Dean'] my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob'] my_func() # ๐Ÿ‘‰๏ธ ['Carl', 'Dean']

We had to use the global statement at the top of the function's definition in order to reassign the global employees variable within the function.

# You have to use global to delete a global variable

You also have to use the global statement if you have to delete a global variable from within a function.

main.py
employees = ['Alice', 'Bob'] def my_func(): global employees print(employees) del employees my_func() # ๐Ÿ‘‰๏ธ ['Alice', 'Bob'] # โ›”๏ธ NameError: name 'employees' is not defined my_func()

Once we mark the employees variable as global, we are able to use the del statement to delete the variable from within the function.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev