【发布时间】:2015-11-25 14:29:49
【问题描述】:
我正在尝试从模块返回字典,但无论在 Windows 7 上的 Python2.7 上如何,字典都返回为空。例如:
test_dict.py
def get_dict():
dictiom = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print dictiom['Name']
return dictiom['Name']
if __name__ == '__main__':
get_dict()
get_dict.py
import test_dict
dict_test = test_dict.get_dict()
print dict_test
独立运行test_dict.py,打印语句返回
Zara
从 get_dict.py 打印调用 dict
{}
这是一个简化的示例,但对于我尝试过的其他测试也是如此。
更新以更改我的示例错误。
好的,这是我实际代码的问题部分
搜索.py
from Bio import Entrez
def search(query):
Entrez.email = 'me@example.com'
handle = Entrez.esearch(db='pubmed',
sort='relevance',
retmax='50',
retmode='xml',
term=query)
results = Entrez.read(handle)
return results
main.py
import Search
query = 'cancer'
results = Search.search(query)
print(results)
results 是一个空字典,但无法找出原因。但是单独运行 Search.py 是可行的。
【问题讨论】:
-
dict()已经是用于创建字典的内置函数...不要为函数/变量使用内置名称 -
好点,我在把这个例子放在一起时忘记了这一点。我的真实代码没有使用 dict 示例。
标签: python dictionary