How to make better globals variables on python

Well everyone knows what global <something> does on python, and how to set it, by placing the variable flying around the main file, but how to make it looks better and how to avoid issues with it? well i mean what if your global is called numbers but you set it in a funcion? it is a problem! but i have a solution for these global variables, and it is importing scripts.

How it works?

So, python uses from foo import * as baa just as a global variable, lets say on foo.py you have:

foo.py

save_data = {}
health = 10
damage = 2

main.py

from foo import * as baa

print(baa.health) # access health from foo

How the import works

the import is very simple, “import foo” tells python to get data from foo, everything to be precise, but to get the data you will need to use the file name itself, so it will be “foo.something”, but when adding “import *” it will get everything and import “raw” (like if the script was written in the same file) whiout the need of foo.something. so as baa will make it work like foo.something again, but now with your own custom name.

Leave a Comment