源码

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


我们假设我有以下文件结构:

data.py

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

core.py

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

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

(1)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/ruhehuoqutedingpythonmokuaizhongdebianliangliebiao.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 26, 2019 at 01:17 下午

热评文章