Python 使用全局变量

时间:2023-02-12 08:26:50

今天在写以下Python代码时出现了问题。该段代码根据一些URL下载视频:

#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声明,但是每一次用到类变量的时候都需要冠以类名。