从使用星号导入的模块中获取属性

时间:2016-08-02 09:59:45

标签: python python-3.x python-3.5

python脚本需要每用户配置来覆盖(重新定义)“默认”分配(例如path = "local/path/"),这可以通过使用from custom_settings import *导入自定义模块的属性来完成。我想输出自定义模块正在更改的那些。

custom_settings内,可以使用dir()获取其属性,例如", ".join( (a for a in dir() if not a.startswith("__") ) )。 如果导入的模块属性绑定到现有命名空间,如何从导入脚本完成?

1 个答案:

答案 0 :(得分:0)

这可能不是最好的解决方案,看起来很痛苦,但你可以随时简单地说:

from custom_settings import *
import custom_settings
overridden = ", ".join(a for a in dir(custom_settings) if not a.startswith("__"))