如果以后遇到一个类,并且实例化对象后可以设置key 和value:我们应该想到这个类可能有__setitem__(self,key,value)方法,或者这个类继承了Dict类
如果重写了构造方法:一定要执行dict构造方法
class Func(dict):
def __init__(self,val):
dict.__init__(self,val)
flask.config.Config对象(继承字典),默认配置为:
{ ‘DEBUG‘: get_debug_flag(default=False), 是否开启Debug模式 ‘TESTING‘: False, 是否开启测试模式 ‘PROPAGATE_EXCEPTIONS‘: None, ‘PRESERVE_CONTEXT_ON_EXCEPTION‘: None, ‘SECRET_KEY‘: None, ‘PERMANENT_SESSION_LIFETIME‘: timedelta(days=31), ‘USE_X_SENDFILE‘: False, ‘LOGGER_NAME‘: None, ‘LOGGER_HANDLER_POLICY‘: ‘always‘, ‘SERVER_NAME‘: None, ‘APPLICATION_ROOT‘: None, ‘SESSION_COOKIE_NAME‘: ‘session‘, ‘SESSION_COOKIE_DOMAIN‘: None, ‘SESSION_COOKIE_PATH‘: None, ‘SESSION_COOKIE_HTTPONLY‘: True, ‘SESSION_COOKIE_SECURE‘: False, ‘SESSION_REFRESH_EACH_REQUEST‘: True, ‘MAX_CONTENT_LENGTH‘: None, ‘SEND_FILE_MAX_AGE_DEFAULT‘: timedelta(hours=12), ‘TRAP_BAD_REQUEST_ERRORS‘: False, ‘TRAP_HTTP_EXCEPTIONS‘: False, ‘EXPLAIN_TEMPLATE_LOADING‘: False, ‘PREFERRED_URL_SCHEME‘: ‘http‘, ‘JSON_AS_ASCII‘: True, ‘JSON_SORT_KEYS‘: True, ‘JSONIFY_PRETTYPRINT_REGULAR‘: True, ‘JSONIFY_MIMETYPE‘: ‘application/json‘, ‘TEMPLATES_AUTO_RELOAD‘: None, }
方式一:
app.config[‘SESSION_COOKIE_NAME‘] = ‘session_lvning‘ #直接在应用中的config["xxx"] 写键值对
方式二:
app.config.from_pyfile(‘settings.py‘)
方式三:
import os os.environ[‘FLAKS-SETTINGS‘] = ‘settings.py‘ app.config.from_envvar(‘FLAKS-SETTINGS‘)
方式四:
app.config.from_object(‘settings.DevConfig‘) #可以更方便地切换测试环境,开发环境或者线上环境等 settings.py class BaseConfig(object): NNN = 123 class TestConfig(BaseConfig): DB = ‘127.0.0.1‘ class DevConfig(BaseConfig): DB = ‘192.168.1.1‘ class ProConfig(BaseConfig): DB = ‘47.18.1.1‘