Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

How to get a list of variables in specific Python module?

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']

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.

question from:https://stackoverflow.com/questions/9759820/how-to-get-a-list-of-variables-in-specific-python-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
print [item for item in dir(adfix) if not item.startswith("__")]

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

Why?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...