Is putting variables from .env/.ini/ in Enum good?

Viewed 43

According to PEP 435 Enum class is

An enumeration is a set of symbolic names bound to unique, constant values. 
Within an enumeration, the values can be compared by identity, and the enumeration itself can be iterated over.

Should we store variables from .env, .ini or environmental variables in such a class, or is it overkill?

from enum import Enum
from decouple import config


class Constants(Enum):
    username = config('DATABASE_USERNAME')
    password = config('DATABASE_PASSWORD')
    host = config('DATABASE_HOST')
    port = config('DATABASE_HOST', cast=int)

Is the above a good idea in the ,,art of code''? Or maybe the better approach is to call each time config function whenever we need that variable in code?

1 Answers

If the configuration values do not change during the run of the program -- in other words, they are constant while the program is running -- and you want to be able to access them is

do_a_thing(Constants.host.value, Constants.port.value)

then sure, there's nothing wrong with doing it that way.

Related