代码如下:

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. JavaWeb笔记(十二)日志

    日志 日志信息根据用途与记录内容的不同,分为调试日志.运行日志.异常日志等. Java常用记录日志 logger log4j log4j2 logback 其中除了logger使用的概率较小,因此主要 ...

  2. android命令模式IntentService 远程下载文件

    服务可用在一下情景: 1,用户离开activity后,仍需要继续工作,例如从网络下载文件,播放音乐. 2,无论activity出现或离开,都需要持续工作,例如网络聊天应用. 3,连接网络服务,正在使用 ...

  3. mysql ibd 文件还原数据

    -- 这里要还原的表名为 test_table -- 1建库,并选中库,库名随意 -- 2查看InnoDB 引擎独立表空间是否开启 SHOW VARIABLES LIKE '%per_table%' ...

  4. 在C/C++程序中打印当前函数调用栈

    前几天帮同事跟踪的一个程序莫名退出,没有core dump(当然ulimit是打开的)的问题.我们知道,正常情况下,如果程序因为某种异常条件退出的话,应该会产生core dump,而如果程序正常退出的 ...

  5. 一种保持顺序的Properties

    其实properties有没有顺序都一样 程序都能正常运行 但看着就比较闹心 所以网上找了找 还真有人给了个例子实现读Property的有序 但是删除某些属性之后 写入又有问题 会异常 后来重写了一下 ...

  6. jloi2017(shoi2017?)六省联考酱油记

    Day -n 听说了4.22.4.23的省选,而且还是六省联考. 压力山大. 尽管我只是一名高一的simple OIer,在省选到来之前,心里还是很紧张的. 毕竟自己也知道南方dalao们都是神犇,像 ...

  7. BZOJ2460 [BeiJing2011]元素 【线性基】

    2460: [BeiJing2011]元素 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1675  Solved: 869 [Submit][St ...

  8. [hdu 4348]区间修改区间查询可持久化线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...

  9. POJ1511:Invitation Cards(最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 34743   Accepted: 114 ...

  10. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...