帮助访问Python函数内的模块变量?
我在名为 dictbuilder.py 的 Python 模块中有以下函数:
def my_dictbuilder(reader_o, writer):
fieldnames = ('name', 'number')
reader = csv.DictReader(reader_o, fieldnames = fieldnames, delimiter="\t")
my_dict = {}
id = 0
for row in reader:
id += 1
my_dict[id] = row['name'], row['number']
id += 1
return(my_dict)
我已从名为 main.py 的模块导入并调用了该函数。我还想使用已导入 main.py 的 dictbuilder.py 模块中的 my_dict
变量。当我尝试打印 my_dictbuilder.mydict
时,出现此错误:
AttributeError: 'function' object has no attribute 'my_dict'
谁能帮我弄清楚如何访问 my_dict我的 main.py 文件中的变量?感谢您的帮助!
I have the following function in a Python module called dictbuilder.py:
def my_dictbuilder(reader_o, writer):
fieldnames = ('name', 'number')
reader = csv.DictReader(reader_o, fieldnames = fieldnames, delimiter="\t")
my_dict = {}
id = 0
for row in reader:
id += 1
my_dict[id] = row['name'], row['number']
id += 1
return(my_dict)
I have imported and called this function from a module called main.py. I would like to also use the my_dict
variable from the dictbuilder.py module I have imported into main.py. When I try to printmy_dictbuilder.mydict
I get this error:
AttributeError: 'function' object has no attribute 'my_dict'
Can anyone help me figure out how to access the my_dict variable from my main.py file? Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您正在返回
my_dict
,因此在调用它时只需将返回值存储在主模块中即可。Well, you're returning
my_dict
, so just store the return value in your main module when you call it.