Can I use __init__.py to define global variables?

后端 未结 4 2016
一生所求
一生所求 2020-11-29 17:24

I want to define a constant that should be available in all of the submodules of a package. I\'ve thought that the best place would be in in the __init__.py fil

4条回答
  •  遥遥无期
    2020-11-29 18:10

    Just wanted to add that constants can be employed using a config.ini file and parsed in the script using the configparser library. This way you could have constants for multiple circumstances. For instance if you had parameter constants for two separate url requests just label them like so:

    mymodule/config.ini
    [request0]
    conn = 'admin@localhost'
    pass = 'admin'
    ...
    
    [request1]
    conn = 'barney@localhost'
    pass = 'dinosaur'
    ...
    

    I found the documentation on the Python website very helpful. I am not sure if there are any differences between Python 2 and 3 so here are the links to both:

    For Python 3: https://docs.python.org/3/library/configparser.html#module-configparser

    For Python 2: https://docs.python.org/2/library/configparser.html#module-configparser

提交回复
热议问题