Python 使用全局变量

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

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值