gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:25:31 29 4
gpt4 key购买 nike

假设我有以下文件结构:

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com