从用s导入的模块中获取属性

2024-04-24 05:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

custom_settings中,它的属性可以通过^{}获得,例如", ".join( (a for a in dir() if not a.startswith("__") ) )。 如果导入的模块属性绑定到现有名称空间,那么如何从导入脚本执行此操作?你知道吗


Tags: 模块path用户infromimport脚本for
1条回答
网友
1楼 · 发布于 2024-04-24 05:01:18

这可能不是最整洁的解决方案,而且似乎是令人痛苦的冗余,但您可以始终简单地:

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

相关问题 更多 >