Python file structure

zljubisicmob at gmail.com zljubisicmob at gmail.com
Tue May 12 15:13:20 EDT 2015


Hi, I have python file with the following structure:

import...

A = configparser.get(...) 
B = configparser.get(...)

Command line parameters parsing [they can change variable A or B]

Def usage()
	Print how to use script parameters

def main():
	...

if __name__ == "__main__":
    main()

If I find an error in command line parameters section I cannot call function usage() because it is not defined yet. 

I have few options here:
1.	Put definition of usage function before command line parameters parsing section
2.	Make parameters global and put them in the main function
3.	...maybe some other options...

The basic idea is that variables A and B should be accessible globally, but after they are read by configparser and/or changed by command line parameters parsing section, the variables should preserve their values throughout the whole program.

If I set them in the way described above, even the variables are not global I can read their values everywhere with no option to change them.
If I set them with global keyword I am risking changing their values afterwards. 

What approach do you suggest?
The goal is to have global variables that should preserve their values and at the same time no meter if they are defined by configparser and/or command line parameter to be able to print usage if there is a problem with a parameter?

Regards.



More information about the Python-list mailing list