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:
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']
Create global variables at the beginning of the file and call those throughout the program. Downside: ruins the encapsulation of the classes.
Something else.
What is the most pythonic way of doing this?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With