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

后端 未结 4 1444
天涯浪人
天涯浪人 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:39

    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

提交回复
热议问题