重置变量

时间:2014-01-04 18:24:54

标签: python

所以我试图让我的Point模块在游戏结束后重置这些变量,这些变量超出了类Point:

    lotteryStart = time.time()
    players = []
    pot = 1

class Point:

     def load():
            # load the database into the bot
        dPoint = {} # dict for points
        for name in open("Point.DB","r").readlines():
           if len(name.strip())>0:
             name,point = name.split()
             dPoint[name] = int(point)
        Point.dPoint = dPoint
        Point.MINUTE = 60
        Point.HOUR = Point.MINUTE * 60
        Point.DAY = Point.HOUR * 24
        Point.YEAR = Point.DAY * 365 
        Point.timer = 15
        lotteryStart = time.time()
        pot = 1
        players = []

我尝试将相同的变量放在我的def load()中:这是在类下面但是当使用Point.load()时,变量保持与加载前相同。有没有办法让它将变量设置为原始开始。

1 个答案:

答案 0 :(得分:1)

以下代码将更改全局变量'x':

x = 1;
class Point:
    x = 2
print x # will print 1

为了解决这个问题,你必须在所需的类或函数中明确声明'global x':

x = 1;
class Point:
    global x
    x = 2
print x # will print 2