Global Variable
noun
say it: “GLOH-bul VAIR-ee-uh-bul” /ˈɡloʊbəl ˈvɛəriəbəl/
A variable created outside any function, so code anywhere in the file can read it; changing one inside a function takes the global keyword.
What is a global variable?
A global variable is created outside any function, so code anywhere in the file can read it. Changing one from inside a function takes the global keyword, so it's usually better to pass values in and return them.
STORE_NAME = "Corner Cafe"
def print_receipt():
print(f"Thanks for visiting {STORE_NAME}")
print_receipt() # Thanks for visiting Corner Cafe
In a sentence
- “STORE_NAME is a global variable every function can read.”
- “Passing the total in and returning it beats changing a global variable.”
Level: Apprentice · More Python terms at this level
Updated