首页 > 编程语言 > 详细

Python 修改global 变量

时间:2015-09-15 12:52:03      阅读:204      评论:0      收藏:0      [点我收藏+]

今天在写一个脚本时需要定义一个全局的时间变量,但是在函数中修改后一直不能更新,发现是因为函数是有自己的namespace

 

last_send_time = 0

def test():
    last_send_time = 2

print last_send_time

#will print 0



#to change it.


def test():
    global last_send_time
    last_send_time = 2

print last_send_time

 

 

From: stackoverflow

Your issue is that functions create their own namespace, which means that done within the function is a different one than done in the second example. Use global done to use the first done instead of creating a new one.

def function():
    global done
    for loop:
        code
        if not comply:
            done = True

An explanation of how to use global can be found here

Python 修改global 变量

原文:http://www.cnblogs.com/-Doraemon/p/4809690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!