gpt4 book ai didi

Python - 填充包含来自另一个模块的变量的列表

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:15 24 4
gpt4 key购买 nike

Python 中有没有一种方法可以将模块中的变量编译到列表中,而无需手动执行?

例如:

#catName.py
cat1 = "toby"
cat2 = "bill"
cat3 = "sally"
catList = [cat1,cat2,cat3]

#printCatNames.py
import catName
for names in catName.catList:
print names

我正在尝试做的(使用上面的示例)是在调用模块 catName 时自动填充 catList。如果有人能指出我正确的方向,将不胜感激。

最佳答案

import  catName

n [3]: [v for k,v in catName.__dict__.items() if not k.startswith("__")]
Out[3]: ['toby', 'sally', 'bill']

我个人会坚持手动填写列表。

关于Python - 填充包含来自另一个模块的变量的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29738184/

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