What's the best way to initialise and use constants across Python classes?

后端 未结 4 1442
天涯浪人
天涯浪人 2020-12-30 18:46

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         


        
4条回答
  •  一生所求
    2020-12-30 19:37

    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.

提交回复
热议问题