python - 如何获取特定 Python 模块中的变量列表?

标签 python

假设我有以下文件结构:

data.py

foo = []
bar = []
abc = "def"

core.py

import data
# do something here #
# a = ...
print a
# ['foo', 'bar', 'abc']

我需要获取 data.py 文件中定义的所有变量。我怎样才能做到这一点?我可以使用 dir(),但它会返回模块的所有属性,包括 __name__ 等等。

最佳答案

print [item for item in dir(adfix) if not item.startswith("__")]

通常是这样做的秘诀,但它引出了问题。

为什么?

关于python - 如何获取特定 Python 模块中的变量列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9759820/

相关文章:

python - 如何使用另一个数组中的值更新 numpy 数组的所有值

Python:二分法

仅当从上下文菜单调用时,Python 脚本才会在 subprocess.run() 调用上执行失败

python - 具有多个查找参数的 django-rest-framework HyperlinkedIdentityField

python - 替换单行中的所有正则表达式匹配

python - 减少从大型 SQLAlchemy 数据库中检索行的时间

python - 我怎样才能找到一个对象被实例化的地方?

python - 生成进口图

Python - 不确定如何将列中的行值汇总到列表中

Python:将耗时(分:秒)解析为秒