4

What is the standard way of declaring configuration variables for your program at the top of a python script? These are used throughout the program in multiple classes and functions. Is the best way:

  1. To create a mixed dictionary with the configuration options, and pass these to any classes that need them. Downside: this requires passing extra attributes. For example:

    config = {'parseTags': {'title','font','p'},    
        'name': 'steve',                
        'logFrequencies': 10,               
        'print_rate': False  
        }
    

     

    newCustomObject = CustomClass(config)
    customfunction(config)
    print 'hi',config['name']
    
  2. Create global variables at the beginning of the file and call those throughout the program. Downside: ruins the encapsulation of the classes.

  3. Something else.

What is the most pythonic way of doing this?

2
  • This depends somewhat on the size of your script and how often you need to change the configuration parameters. Is your script small? Will its functions/classes ever be used by other scripts or is it meant to be self contained? Commented Jun 26, 2012 at 14:44
  • It's a small 500-line script that fits in a single file. I don't plan to reuse any of the functions elsewhere, but you never know, and but it would be nice to have an elegant solution that follows Object Oriented Programming principles. I don't want to have a separate file with the configuration options, I would like them in the script. Commented Jun 26, 2012 at 15:31

3 Answers 3

4

For constants used in a single file (module), I usually just declare global variables at the top. This does not lose encapsulation in any meaningful way (it's no different from putting those constants in a base class that every class inherits from).

The django config system provides a nice way to create shared constants: you create a module, and the config system creates a read-only object from it, exposing the members of the module.

Sign up to request clarification or add additional context in comments.

2 Comments

The django config example is quite good. There's nothing stopping you from adding config in a python module. However, if you want your settings to be "live" (i.e. changes to a config file have direct impact on a running service which monitors the config file for changes) the py module approach wouldn't work.
@IoanAlexandruCucu Quite. But neither would the options suggested by OP, so I assume that's not a requirement.
4

Well, option 1 vs option 2 isn't actually that big of a difference. The dictionary from option 1 is itself global and the values in the dictionary are hardcoded.

I don't know what the most pythonic approach would be, but I would pull the configuration in a separate config file and use python's standard ConfigParser module to read it.

Comments

1

I find it most readable when class is used and configuration options are accessed as its memebers (not instance members), that is:

class config:
    option = 3

# code code code
foo = config.opt

You can then easily put that out of the file to a module if it gets big and just do import config from config.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.