You need to declare them global using the global keyword. The global keyword allows you to change the value of a variable that is defined inside the module, outside of any function or class.
You can always print global variables without declaring them, and you can modify global mutable containres like lists and dicts without declaring them global, but you cannot assign values. Heres the official documentation, below is a working example.
a = None
b = None
def somefunc():
# This works.
print a # None
print b # None
def main():
global a, b
a = 'whatever'
b = 10
# somefunc() exist in the local scope, you don't need the getattr.
# just call it.
somefunc()
if __name__ == '__main__': # You were missing the undescores surrounding main
main()
Demo output:
msvalkon@Lunkwill:/tmp$ python t.py
whatever
10
It's a little bad practice to use globals. They have their use cases but usually there is a better way, like passing the data as arguments to functions or wrapping all functions inside a class which holds the value you'd normally make global. Globals are great for storing static information, like configuration info, a server address that doesn't change, a port number and so forth. You should try to avoid using them like normal variables.