简体   繁体   中英

Constants in a python package

I'm writing a python package and am wondering where the best place is to put constants?

I know you can create a file called 'constants.py' in the package and then call them with module.constants.const, but shouldn't there be a way to associate the constant with the whole module? eg you can call numpy.pi, how would I do something like that?

Also, where in the module is the best place to put paths to directories outside of the module where I want to read/write files?

Put them where you feel they can most easily be maintained. Usually that means in the module to which the constants logically belong.

You can always import the constants into the __init__.py file of your package to make it easier for someone to find them. If you did decide on a constants module, I'd add a __all__ sequence to state what values are public, then in the __init__.py file do:

from constants import *

to make the same names available at the package level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM