I group and store my constants (can be a dict.) in a separate python file. E.g. parameters.py :
HARD_COPY=True
BOOKS={
2018:{"author":"Hugo"}
}
etc.
In a source file, I write :
import parameters as para
Of course, I can use the variables knowing their names (para.BOOKS, etc.). But I wish to display their names and values, whatever those names can be.
If I write dir(para), I can see the list of these names. But how can I print their values ?
I'd like to display :
My defined variables in parameters.py :
HARD_COPY : True
BOOKS : {2018:{"author":"Hugo"}}
etc.
Use getattr
to access its value by name.
import parameters as para
print('My defined variables in parameters.py : ')
for variable in dir(para):
if not variable.startswith('__'):
print(variable, ':', getattr(para, variable))
Output:
My defined variables in parameters.py :
BOOKS : {2018: {'author': 'Hugo'}}
HARD_COPY : True