Python Clean Code – Declaring Constants: One File or Separate?

clean codepython

I currently have a small list (< 20) of constants that can be segmented into the following three parts:

- main script (tokens, log file location)
- database setup (username, passwords)
- API (API key, various external IDs)

I felt that it cluttered the top of the files during declaration, so I transferred them into a constants file. However, when importing them back to the three files, for instance say the database setup, it would become

import constants

...

db = connect(constants.RDS_HOSTNAME, constants.RDS_PASSWORD)

compared to if I had declared them in the file

RDS_HOSTNAME = 'xxx'
RDS_PASSWORD = 'yyy'

...

db = connect(RDS_HOSTNAME, RDS_PASSWORD)

which I feel is a bit more readable when using the constants.

If I combine the both, I would have

from constants import *

...

db = connect(RDS_HOSTNAME, RDS_PASSWORD)

but all posts I've read suggest very heavily against this as it would increase confusion due to not knowing where the constants come from.

My question is this: given that I have only one constants file with no functions, and all constants are upper case, would using from constants import * nonetheless be poor practice? Due to the uppercase notation, I would know immediately it's a constant and comes from the constants file, thus reducing confusion.

Best Answer

All the “constants” you listed have nothing to do in your code. They belong to the configuration.

Think it this way. Your code contains the logic. Whenever you change the logic, you change your code. Whenever you change information about the world, you don't change your code.

If this is still unclear, think about somebody taking your code and running it in a very different context. For instance you created a bulletin board system and you run it on your local server, connecting to the specific PostgreSQL database and using Amazon S3 to store uploaded files. You published the source code on GitHub, which allows me to grab the code and run it on my own infrastructure, using my own PostgreSQL database and my own S3 bucket (you won't enjoy paying Amazon to let me store this data in your bucket, do you?) Therefore, GitHub will contain the logic; configuration files, on the other hand, would be kept private on your servers, and should never reach GitHub (unless you really want people to use your S3 bucket for free).

API keys, passwords and log file locations should therefore be in configuration files, and constants should be kept for things which are constant within the scope of your logic. If, say, your logic forces you to have specific types of users, for instance administrators—designed as Administrator in your code, ordinary users—Contributor, and finally anonymous users—Guest, then Administrator, Contributor and Guest are the constants, because they are part of your business logic. In this case, you'll put them in the file in which they belong, possibly in or near the class which represents the user or more specifically the type of the user.

Related Topic