如何获取特定 Python 模块中的变量列表? [英] How to get a list of variables in specific Python module?

查看:57
本文介绍了如何获取特定 Python 模块中的变量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下文件结构:

Let's assume I have the following file structure:

data.py

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

core.py

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

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

I need to get all the variables defined in data.py file. How can I achieve that? I could use dir(), but it returns all the attributes of the module including __name__ and so on.

推荐答案

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

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

Is usually the recipe for doing this, but it begs the question.

这篇关于如何获取特定 Python 模块中的变量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆