There are instances where a variable is practical to be declared as visible throughout the script (or be global) to avoid having to pass it as an argument, among other reasons. In my case, it is often a log file reference so that it can be written to from anywhere in the script without being explicitly passed but I am sure there are many other uses.
I have accomplished this in different ways:
#!/bin/python3
################################################
# script var declaration outside classes and functions
################################################
# method 1
global globalVarOne
# method 2
globalVarTwo = None
################################################
# class SomeClass
################################################
class SomeClass(object):
#both globalVarOne and globalVarTwo
#are equally visible
################################################
# def someFunction
################################################
def someFunction(argOne, argTwo):
#both globalVarOne and globalVarTwo
#are equally visible
################################################
# main
################################################
if __name__ == '__main__':
#...
What is the difference between declaring such variables as global
vs just setting them to None
in a statically invoked block of the script? Are there other (better) ways to accomplish the same? Also, is there a pythonic name for that part of the script, which is not a part of the main or any class or function?