我有一个Python模块,它初始化一些全局变量,就像这样:
#!/usr/bin/env python
import re
"""My awesome python library."""
# A word list from the standard UNIX dictionary.
with open('/usr/share/dict/words', 'rt') as f:
WORDS_LIST = f.read().split('\n') + ['http'] + ['fubob']
# Some compiled regular expressions.
COMPILED_REG1 = re.compile("a")
COMPILED_REG2 = re.compile("b")
# Some constants.
A = 10
B = 20
def say_hello(): print('hello')
def do_something(): return 'something'
当然这样做是可行的,但我觉得这不是正确的方式:每次我导入这个模块时,Python都会执行它。在这个例子中,它将读取文件并编译正则表达式。
我看到有些人会创建一个名为config.py的文件,并对其进行一些操作,但我不确定这具体是如何操作的。
所以,如果您需要创建一个标准的Python库,我想知道您会如何处理这个问题。