Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90480

Python file structure

Newsgroups comp.lang.python
Date 2015-05-12 12:13 -0700
Message-ID <f25aa9d4-4025-457d-8072-5327c98db1bd@googlegroups.com> (permalink)
Subject Python file structure
From zljubisicmob@gmail.com

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Python file structure zljubisicmob@gmail.com - 2015-05-12 12:13 -0700
  Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 05:29 +1000
  Re: Python file structure Ned Batchelder <ned@nedbatchelder.com> - 2015-05-12 12:49 -0700
    Re: Python file structure zljubisicmob@gmail.com - 2015-05-12 12:58 -0700
      Re: Python file structure Dave Angel <davea@davea.name> - 2015-05-12 16:43 -0400
    Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 06:02 +1000
    Re: Python file structure Terry Reedy <tjreedy@udel.edu> - 2015-05-12 17:34 -0400
  Re: Python file structure Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-12 13:54 -0600
  Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 06:07 +1000
  Re: Python file structure Tim Chase <python.list@tim.thechases.com> - 2015-05-13 13:34 -0500
  Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-14 12:03 +1000

csiph-web