Python 使用全局变量
2017-05-31 16:13
351 查看
今天在写以下Python代码时出现了问题。该段代码根据一些URL下载视频:
但是在运行时报以下错误:
意思是说case_total_num为局部变量,在使用它之前没有被赋值。Python并没有按照我的意图将case_total_num当成全部变量。看来在函数内无法直接使用全局变量。
有什么办法能在函数内使用全局变量呢?根据官方文档,可以用global语句:
The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global.
意思是说global语句可以声明一个或多个变量为全局变量。该声明仅在当前代码块中有效。除此之外,没办法访问全局变量。
于是,在上面的代码中加入global声明语句就可以达到目的了:
用global声明多个变量需要用逗号分隔:
在使用全局变量的场合,也可用类变量代替:
这样便去掉了global声明,但是每一次用到类变量的时候都需要冠以类名。
#coding=utf-8 def save_case_infos(case_urls): for case_url in case_urls: # some download code ... # 打印已下载的视频个数 # 希望访问全局变量 case_total_num case_total_num += 1 print("current count: " + str(case_total_num)) # 全局变量,表示已下载的视频个数 case_total_num = 0 # 下载视频文件 urls = [r'http://cynhard.com/live/001', r'http://cynhard.com/live/002', r'http://cynhard.com/live/003'] save_case_infos(urls)
但是在运行时报以下错误:
Traceback (most recent call last): File "G:\projects\python\globaltest.py", line 21, in <module> save_case_infos(urls) File "G:\projects\python\globaltest.py", line 10, in save_case_infos case_total_num += 1 UnboundLocalError: local variable 'case_total_num' referenced before assignment
意思是说case_total_num为局部变量,在使用它之前没有被赋值。Python并没有按照我的意图将case_total_num当成全部变量。看来在函数内无法直接使用全局变量。
有什么办法能在函数内使用全局变量呢?根据官方文档,可以用global语句:
The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global.
意思是说global语句可以声明一个或多个变量为全局变量。该声明仅在当前代码块中有效。除此之外,没办法访问全局变量。
于是,在上面的代码中加入global声明语句就可以达到目的了:
#coding=utf-8 def save_case_infos(case_urls): global case_total_num # global声明 for case_url in case_urls: # some download code ... # 打印已下载的视频个数 # 希望访问全局变量 case_total_num case_total_num += 1 print("current count: " + str(case_total_num)) # 全局变量,表示已下载的视频个数 case_total_num = 0 # 下载视频文件 urls = [r'http://cynhard.com/live/001', r'http://cynhard.com/live/002', r'http://cynhard.com/live/003'] save_case_infos(urls)
用global声明多个变量需要用逗号分隔:
#coding=utf-8 def save_case_infos(case_urls): global succeeded_total_num, failed_total_num # 多个变量用逗号分隔 for case_url in case_urls: ok = False # some download code ... if ok: succeeded_total_num += 1 else: failed_total_num += 1 print("succeeded: " + str(succeeded_total_num) + " failed: " + str(failed_total_num)) # 全局变量,下载成功和失败的视频个数 succeeded_total_num = 0 failed_total_num = 0 # 下载视频文件 urls = [r'http://cynhard.com/live/001', r'http://cynhard.com/live/002', r'http://cynhard.com/live/003'] save_case_infos(urls)
在使用全局变量的场合,也可用类变量代替:
#coding=utf-8 def save_case_infos(case_urls): for case_url in case_urls: ok = False # some download code ... if ok: G.succeeded_total_num += 1 else: G.failed_total_num += 1 print("succeeded: " + str(G.succeeded_total_num) + " failed: " + str(G.failed_total_num)) # 将全局使用的变量定义在类中 class G: succeeded_total_num = 0 failed_total_num = 0 # 下载视频文件 urls = [r'http://cynhard.com/live/001', r'http://cynhard.com/live/002', r'http://cynhard.com/live/003'] save_case_infos(urls)
这样便去掉了global声明,但是每一次用到类变量的时候都需要冠以类名。
相关文章推荐
- Python 继承结构中全局变量的使用
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- Python基础学习篇——Global全局变量的使用
- python函数中使用全局变量
- Python: 全局变量 & 局部变量的使用
- python 的全局变量global变量使用
- python中全局变量的使用
- python 全局变量的使用
- 在python中实现生产者和消费者的例子(五):使用threading模块和全局变量
- python3函数内全局变量使用global
- Python 3 实现定义跨模块的全局变量和使用
- python多文件使用同一个全局变量的定义和使用方法
- (原)python中不同文件之间使用所谓的全局变量
- python全局变量使用
- python中全局变量的使用
- Python 3 实现定义跨模块的全局变量和使用
- 讨论下python中全局变量的使用
- Python基础学习篇——Global全局变量的使用
- python使用全局变量(global)
- Python在函数中使用全局变量的问题