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


Groups > comp.lang.python > #74448

Re: initializing "parameters" class in Python only once?

From Ben Finney <ben@benfinney.id.au>
Subject Re: initializing "parameters" class in Python only once?
Date 2014-07-15 10:04 +1000
References <53C4589A.9040109@jpl.nasa.gov>
Newsgroups comp.lang.python
Message-ID <mailman.11815.1405382658.18130.python-list@python.org> (permalink)

Show all headers | View raw


Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> writes:

> The problem:  I'm writing a large Python program and I have a bunch of
> parameters (whose values are static) that I want to make available to
> the rest of the code with minimum overhead and duplicate processing.

Better than putting these in executable code, then, is to put them in a
configuration file read as *data*, not code, when your program starts.

Look at the ‘configparser’ module from the Python standard library
<URL:https://docs.python.org/3/library/configparser.html> for a robust
way to read run-time configuration parameters from a non-executable file.

The result of reading the config file (or set of them, if your use case
is complex enough) is an object containing the parameters, which can be
interrogated as normal by getting its attributes. See the documentation
for examples.

-- 
 \            “[T]he great menace to progress is not ignorance but the |
  `\           illusion of knowledge.” —Daniel J. Boorstin, historian, |
_o__)                                                        1914–2004 |
Ben Finney

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


Thread

initializing "parameters" class in Python only once? Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> - 2014-07-14 15:24 -0700
  Re: initializing "parameters" class in Python only once? Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-07-14 16:07 -0700
  Re: initializing "parameters" class in Python only once? Chris Kaynor <ckaynor@zindagigames.com> - 2014-07-14 16:21 -0700
  Re: initializing "parameters" class in Python only once? Ben Finney <ben@benfinney.id.au> - 2014-07-15 10:04 +1000
  Re: initializing "parameters" class in Python only once? Steven D'Aprano <steve@pearwood.info> - 2014-07-15 05:28 +0000
    Re: initializing "parameters" class in Python only once? alex23 <wuwei23@gmail.com> - 2014-07-17 13:35 +1000
      Re: initializing "parameters" class in Python only once? Steven D'Aprano <steve@pearwood.info> - 2014-07-17 04:27 +0000
      Re: initializing "parameters" class in Python only once? Ethan Furman <ethan@stoneleaf.us> - 2014-07-16 21:43 -0700

csiph-web