70

Here's how I am declaring constants and using them across different Python classes:

# project/constants.py
GOOD = 1
BAD = 2
AWFUL = 3

# project/question.py
from constants import AWFUL, BAD, GOOD

class Question:
    def __init__(self):
        ...

Is the above a good way to store and use contant values? I realise that after a while, the constants file can get pretty big and I could explicitly be importing 10+ of those constants in any given file.

Thierry Lam
  • 45,304
  • 42
  • 117
  • 144

5 Answers5

85

why not just use

import constants

def use_my_constants():
    print constants.GOOD, constants.BAD, constants.AWFUL

From the python zen:

Namespaces are good. Lets do more of those!

EDIT: Except, when you do quote, you should include a reference and check it, because as others have pointed out, it should read:

Namespaces are one honking great idea -- let's do more of those!

This time, I actually copied it from the source: PEP 20 -- The Zen of Python

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 1
    ... Which is also an infinitely more approrpiate way to put it. –  Jun 14 '11 at 15:55
  • @Daniel Roseman: Ouch. I stand corrected. I'll edit my post accordingly! – Daren Thomas Jun 15 '11 at 07:28
  • 1
    Another thing you can do to reduce typing is to use "import constants as CN" and "print(CN.GOOD)" etc. – Jon Coombs Jan 30 '14 at 17:47
  • 2
    For a moment I thought, constants is an actual python library, how nice it would have been, LoL. I didn't read the question. :-) – Whirl Mind Aug 18 '19 at 11:49
  • what is I have Constance with critical static critical data and I want to read it demo ini file and assign it to variable before using it, I tried writing methods but not able to call in in definition also tried with __init__ method but failed. I'm new to python anyway. – Mayur Sep 08 '20 at 20:23
16

You also have the option, if the constants are tied to a particular class and used privately within that class of making them specific to that class:

class Foo(object):
   GOOD = 0
   BAD = 1
   WTF = -1

   def __init__(self...

and away you go.

wheaties
  • 35,646
  • 15
  • 94
  • 131
4

You have a few options:

  1. Do it the way you're doing right now with a file of constants
  2. Use a flat file and parse it once, pass a dictionary / class around
  3. Query off to a database

From an overhead point of view, 1 and 2 are about the same. As for your question about importing specific constants, it's much easier to use one of the following conventions:

  • import constants; some_func(constants.AWFUL)
  • from constants import *; some_func(AWFUL)
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
3

Try to look at the following links(one link contains a receipt from active state of how to implement a real constants):

Importing a long list of constants to a Python file

Create constants using a "settings" module?

Can I prevent modifying an object in Python?

Community
  • 1
  • 1
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
0

I use a function as immutable constant, like this:

def test_immutable_constant():
    def GOOD():
        return 1

    good_value = GOOD()
    good_value = 2

    assert GOOD() == 1
    assert good_value == 2
Wendel
  • 2,809
  • 29
  • 28