代码如下:

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. java文件的I/O

    [原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...

  2. Windows下的Memcache安装与Java部署

    Windows下的Memcache安装: 1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c:\mem ...

  3. float与定位的区别

    float和绝对定位的区别 CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位.1 需要注意的是,使用float脱离文档流时,其他盒子 ...

  4. [bzoj1052] [HAOI2007]覆盖问题

    Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L * L的正方形塑料薄膜 ...

  5. MySQL事物机制具备四点:简称ACID操作

    MySQL事物机制具备四点:简称ACID操作 1.原子性:要么都做,要么都不做(两条数据(写入和存储)一步未成功,整体回滚) 2.一致性:数据库的状态改变(两条数据(写入和存储)均成功,符合原子性,但 ...

  6. BZOJ 2820: YY的GCD | 数论

    题目: 题解: http://hzwer.com/6142.html #include<cstdio> #include<algorithm> #define N 100000 ...

  7. BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap

    一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...

  8. 文件格式转换神器-pandoc

    By francis_hao    Mar 11,2017 介绍 如果你需要在各种类型的文件中穿梭,那么你需要这把瑞士军刀-pandoc 它可以将各种常见的不常见的文件类型转换成另一种,我感兴趣的是在 ...

  9. Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings

    E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...

  10. Phantomjs设置浏览器useragent的方式

    Selenium中使用PhantomJS,设置User-Agent的方法. 默认情况下,是没有自动设置User-Agent的:设置PhantomJS的user-agent def __init__(s ...