def from_object(self, obj):
if isinstance(obj, string_types):
obj = import_string(obj)
for key in dir(obj):
if key.isupper():
self[key] = getattr(obj, key)
在flask app.py的源代码中看到import_string,往上看是出自werkzeug.utils,看了下文档:
-
Imports an object based on a string. This is useful if you want touse import paths as endpoints or something similar. An import path canbe specified either in dotted notation (
xml.sax.saxutils.escape
)or with a colon as object delimiter (xml.sax.saxutils:escape
).If silent is True the return value will be None if the import fails.
Parameters: - import_name – the dotted name for the object to import.
- silent – if set to True import errors are ignored andNone is returned instead.
Returns: imported object
werkzeug.utils.
import_string
(
import_name,
silent=False
)
>>> import_string('config.TestingConfig')
<class config.TestingConfig at 0x0000000002A8F708>