python定义一个全局字典,跨不同文件脚本的Python全局字典

Hello. I am trying to use a global dict created in main.py, which is called in functions.py.

In my main.py I have:

import sys,os,...

import functions.py #import my second file

matrix = {}

matrix_do_something

search_the_matrix(value) #which is defined in functions.py

#FILE: functions.py

def search_the_matrix(value):

global matrix

if value in matrix:

return True

else:

return False

and I get this error:

NameError: global name 'matrix' is not defined

I have read a solution on stackoverflow, which says to put everything in a global file and then call from every file global.matrix[value] but I don't want this. I want just call it matrix and think of it as my global matrix. Is this possible?

Thank you in advance

解决方案

In functions.py you would have to import it

from main import matrix

Though I would want to come up with a better name for my module than main.

If you want an object to be available in a module / file you need to either create it there or import it from somewhere else.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python的requests库是一个用于发送HTTP请求的流行工具。可以使用requests库在Python中发送GET和POST请求,以及上传文件等操作。可以使用requests库来发送HTTP请求,例如获取网页内容、获取IP地址信息等。示例代码如下: ```python import requests r = requests.get('https://github.com/Ranxf') # 最基本的不带参数的GET请求 r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'}) # 带参数的GET请求 ``` 上述代码中,第一个示例发送了一个GET请求,获取了个人GitHub页面的内容;第二个示例发送了一个GET请求并带有参数,使用百度字典API来查询关键字"python"的内容。 此外,requests库还可以用于上传文件。可以使用`requests.post()`方法发送POST请求来上传文件,同时指定`files`参数来指定要上传的文件。示例代码如下: ```python import requests url = 'http://127.0.0.1:8080/upload' files = {'file': open('/home/rxf/test.jpg', 'rb')} # 指定要上传的文件 r = requests.post(url, files=files) print(r.text) ``` 上述代码中,通过`open()`函数打开了一个文件对象,然后将该文件对象作为`files`参数的值传递给`requests.post()`方法,以实现文件上传的功能。 PHP是一种流行的服务器端脚本语言,也可以用于处理HTTP请求。可以使用PHP中的`$_GET`和`$_POST`全局变量来获取GET和POST请求的参数,以及处理上传的文件等操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python——requests模块详解](https://blog.csdn.net/m0_72557783/article/details/128208127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值