Config file with a .py file

前端 未结 4 488
南笙
南笙 2021-01-22 08:39

I have been told that doing this would be a not-very-good practice:

configfile.py

SOUNDENABLED = 1
FILEPATH = \'D:\\\\TEMP\\\\hello.txt\'
         


        
4条回答
  •  心在旅途
    2021-01-22 09:19

    It could be better for some reasons

    1. The only extension that config file could have is py.
    2. You cannot distribute your program with configs in separate directory unless you put an __init__.py into this directory
    3. Nasty users of your program can put any python script in config and do bad things.

    For example, the YouCompleteMe autocompletion engine stores config in python module, .ycm_extra_conf.py. By default, each time config is imported, it asks you, whether you sure that the file is safe to be executed.

    1. How would you change configuration without restarting your app?

    Generally, allowing execution of code that came from somewhere outside is a vulnerability, that could lead to very serious consequences.

    However, if you don't care about these, for example, you are developing web application that executes only on your server, this is an acceptable practice to put configuration into python module. Django does so.

提交回复
热议问题