代码如下:

1.main.py

from flask import Flask
from config import DevConfig app=Flask(__name__) app.config.from_object(DevConfig) @app.route('/')
def home():
return "<h1>Hello Wcf!</h1>" if __name__=='__main__':
app.run()

2.config.py

class Config(object):
pass class ProdConfig(Config):
pass class DevConfig(Config):
DEBUG=True

 想了解一下,app.config.from_boject...是如何运作的,跟踪到源代码中:

在flask的config.py中,有一个方法,是:

    def from_object(self, obj):
"""Updates the values from the given object. An object can be of one
of the following two types: - a string: in this case the object with that name will be imported
- an actual object reference: that object is used directly Objects are usually either modules or classes. :meth:`from_object`
loads only the uppercase attributes of the module/class. A ``dict``
object will not work with :meth:`from_object` because the keys of a
``dict`` are not attributes of the ``dict`` class. Example of module-based configuration:: app.config.from_object('yourapplication.default_config')
from yourapplication import default_config
app.config.from_object(default_config) You should not use this function to load the actual configuration but
rather configuration defaults. The actual config should be loaded
with :meth:`from_pyfile` and ideally from a location not within the
package because the package might be installed system wide. See :ref:`config-dev-prod` for an example of class-based configuration
using :meth:`from_object`. :param obj: an import name or object
"""
if isinstance(obj, string_types):
obj = import_string(obj)
for key in dir(obj):
if key.isupper():
self[key] = getattr(obj, key)

  这里,用到了一个dir的python自带的函数,是其代码的关键,那么,dir是干嘛用的么?

在python中任何东西都是对像,一种数据类型,一个模块等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。

dir()函数操作方法很简单,只需要把你想要查询和对像添写到( )括号中就可以使用了。

flask学习:如何从config里载入配置的更多相关文章

  1. C# 类库下 读取不到config里节点的问题

    前提:要在当前项目下App.config配置信息里读取连接数据库的字符串.忘记创的是个类库.导致一直报错(对象未实例) 解决办法: 程序运行不会读取本项目下的app.config文件(实际它只是个模板 ...

  2. 在web.config里使用configSource分隔各类配置

    转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...

  3. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  4. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  5. Flask 学习篇二:学习Flask过程中的记录

    Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...

  6. Flask学习之五 用户登录

    英文博客地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins 中文翻译地址:http:// ...

  7. [ZHUAN]Flask学习记录之Flask-SQLAlchemy

    From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...

  8. Flask学习【第2篇】:Flask基础

    知识点回顾 flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 实例化Flask对象,里面是有参数的 app = Flask(__name__,template_ ...

  9. 无用之flask学习

    一.认识flask 1.短小精悍.可扩展性强 的一个web框架 注意:上下文管理机制 2.依赖wsgi:werkzurg from werkzeug.wrappers import Request, ...

随机推荐

  1. IDEA使用maven构建时控制台中文乱码的解决办法

    使用maven clean install 项目时控制台中文乱码,解决办法如下: Setting->maven->runner VMoptions: -Dfile.encoding=UTF ...

  2. 福大软工1816:Alpha(1/10)

    Alpha 冲刺 (1/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.自己学习wxpy.pyqt ...

  3. 查看lwjgl常用状态的值

    在遇到状态值较多较复杂的情况,可以选择使用GL11.glIsEnabled()或者GL11.glGet()函数来查看状态机值,以下是常用值: public static void printOpenG ...

  4. Flink之状态之savepoint

    1.总览 savepoints是外部存储的自包含的checkpoints,可以用来stop and resume,或者程序升级.savepoints利用checkpointing机制来创建流式作业的状 ...

  5. ArcGis下的叠加分析

     1矢量与矢量叠加的话就用ToolBox里有Overlay: 2如果是矢量和栅格叠加的话用Spatial analysis模块中的 zonal statistics: 3还有就是栅格与栅格的叠加S ...

  6. Java Integer比较

    今天看微信做了一个选择题,对Integer比较结果有点意外,题目如下: public static void main(String[] args) { Integer a = 1; Integer ...

  7. [洛谷P2384]最短路

    题目大意:给你一个图,要你求出其中1->n路径中乘积最小的一条路 题解:用$log_2$把乘法变成加法,然后记录每个点的前驱,最后求出答案 C++ Code: #include<cstdi ...

  8. 【BZOJ 2432】 [Noi2011]兔农 矩乘+数论

    这道题的暴力分还是很良心嘛~~~~~ 直接刚的话我发现本蒟蒻只会暴力,矩乘根本写不出来,然后让我们找一下规律,我们发现如果我们把这个序列在mod k的意义下摆出,并且在此过程中把值为1的的数减一,我们 ...

  9. <video>标签的特性

    以前的网页视频 过去还没有HTML5的时候,我们处理网页视频的时候,都是通过Flash插件来实现的,然而,并非所有浏览器都有同样的插件. 现在有了HTML5带来的video元素,开发者能够很方便地将视 ...

  10. React.js基础知识

    一. react.js的基本使用方法 (1)快速使用,hello world <div id="app"></div> <script src=&qu ...