码迷,mamicode.com
首页 > 编程语言 > 详细

Python 修改global 变量

时间:2015-09-15 12:53:32      阅读:209      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!