H.Rappeport
H.Rappeport

Reputation: 597

Using one dictionary vs. many to store program configurations

I am writing a Python program with many (approx ~30-40) parameters, all of which have default values, and all should be adjustable at run time by the user. The way I set it up, these parameters are grouped into 4 dictionaries, corresponding to 4 different modules of the program. However, I have encountered a few cases of a single parameter required by more then one of these modules, leading me to consider just unifying the dictionaries into one big config dictionary, or perhaps even one config object, passed to each module. My questions are

  1. Would this have any effect on run time? I suspect not, but want to be sure.

  2. Is this considered good practice? Is there some other solution to the problem I have described?

Upvotes: 0

Views: 334

Answers (1)

Guy
Guy

Reputation: 1360

  1. probably no effect on runtime. larger dictionaries could take longer to lookup in, but in your case, we are talking about 40 items. that's nothing.
  2. we use a single settings file in which we initialize globals by calling a method that either read the config from the environment, a file or a Python file (as globals). the method that reads the config can get the desired type and default value. Others use YAML or TOML for representing configuration and I'm guessing then stores them a globally accessible object. If your settings can be changed in runtime, you have to protect this object in terms of thread-safety (if you have threads of course).

Upvotes: 1

Related Questions