0

I'm very new to python and would like to understand what is the pythonic way to define some contants. I have the following class:

class ConfigBuilder:

   def add_param(self, key, value):
       #...
   def build():
       #...

Now I want to use this in another module as follows:

some_val = #...
config_builder builder = ConfigBuilder()
builder.add_param('PROJECT_RELATED_KEY', some_val)

Where to put this 'PROJECT_RELATED_KEY'? In another module and then import it? Or put it as a global variable?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 3
    It's a matter of preference, wouldn't you say? You could do either, or neither. – cs95 Nov 14 '17 at 19:50
  • @cᴏʟᴅsᴘᴇᴇᴅ I asked this because python seems to me more "conventional" language. Maybe there is some convention for it too. – St.Antario Nov 14 '17 at 19:50
  • 2
    For example: https://stackoverflow.com/a/18225046/4909087 – cs95 Nov 14 '17 at 19:51
  • https://www.python.org/dev/peps/pep-0020/ – wwii Nov 14 '17 at 19:52
  • The only convention is described in [**PEP 8 - Style Guide for Python Code**](https://www.python.org/dev/peps/pep-0008/) in the section on [**Constants**](https://www.python.org/dev/peps/pep-0008/#constants). A good place to put them depends on usage, but generally at the beginning (of a module or function). – martineau Nov 14 '17 at 19:55
  • I try to follow the [Google style guide](https://google.github.io/styleguide/pyguide.html#Global_variables), which doesn't directly address this, but if you look at that link (for global variables), and [this one for naming](https://google.github.io/styleguide/pyguide.html#Naming), it looks like declaring these at the module level is OK if you need it, otherwise class-level is preferred, and you may want to protect it by prepending an underscore, e.g. `_MY_CONST`. It summarizes its stance with "Avoid global variables" though so... – Engineero Nov 14 '17 at 19:58

1 Answers1

1

Agreed that there is no absolute convention for where settings should be stored. There's a lot of flexibility when it comes to project placement. Below are a few possibilities I might consider when deciding where to put my own settings.

If your project might contains some sensitive parameters that you wouldn't want in version control, you could put them into your environment and then access them with environment variables.

import os
print os.environ['HOME']

If they're not sensitive at all, one possible route would be to do what Django does and create a settings.py file. Something like this

# myproject/settings.py
SOME_VALUE = 'SomeConfigurableValue'
FOO = 'bar'
PROJECT_RELATED_KEY = 'bestkeyever'

# myproject/builder.py
from myproject.settings import SOME_VALUE
print(SOME_VALUE)

You could even set up the project as a package and then do

from . import settings
print(settings.SOME_VALUE)

For more info on setting up a project as a package, see: How to fix "Attempted relative import in non-package" even with __init__.py

Robert Townley
  • 3,414
  • 3
  • 28
  • 54