简体   繁体   English

尝试存储类变量以供以后使用

[英]Trying to store a class variable for later use

I'm trying to make a game, and I want to save the position of the player before he moves (oldPosition), so that I can use it later. 我正在尝试制作游戏,我想在玩家移动之前保存其位置(oldPosition),以便以后使用。 The problem is that by the time World.UpdateContents() runs, oldPosition is updated along with self.position. 问题在于,到World.UpdateContents()运行时,oldPosition随self.position一起更新。 How can I keep oldPosition from changing when self.position does? 当self.position改变时,如何保持oldPosition不变?

def Move(self, direction):
    oldPosition = self.position
    print oldPosition
    if direction == "Up":
        if self.position[0] - 1 in World.worldMap[0]:
            self.position[0] -= 1
    if direction == "Down":
        if self.position[0] + 1 in World.worldMap[0]:
            self.position[0] += 1
    if direction == "Left":
        if self.position[1] - 1 in World.worldMap[1]:
            self.position[1] -= 1
    if direction == "Right":
        if self.position[1] + 1 in World.worldMap[1]:
            self.position[1] += 1
    print oldPosition
    World.UpdateContents(world, oldPosition, self.position, self.icon)

The attribute position is a list, when you assign it to oldPosition you're creating a reference to that list, but any modification you make on position will also be reflected on oldPosition - they're pointing to the same object. 属性position是一个列表,当您将其分配给oldPosition您将创建对该列表的引用,但是您对position所做的任何修改也将反映在oldPosition -它们指向相同的对象。 To solve this problem, make a copy of position at the beginning, like this: 要解决此问题,请在开头复制position ,如下所示:

oldPosition = self.position[:]

Now oldPosition is a new, different list and it won't be affected by the changes made to position . 现在oldPosition是一个新的不同列表,它不会受到对position所做的更改的影响。

you need to do oldPosition = list(self.position), assuming that self.position is a list. 假设self.position是一个列表,则需要执行oldPosition = list(self.position)。 This way, oldPosition is not only a reference to self.position but a new list with same values. 这样,oldPosition不仅是对self.position的引用,而且是具有相同值的新列表。

Oscar Lopez is right, and his solution will work. 奥斯卡·洛佩兹(Oscar Lopez)是对的,他的解决方案将起作用。 The problem you're facing is due to the fact that lists in python are mutable. 您面临的问题是由于python中的列表是可变的。 What does this mean? 这是什么意思?

Well, say we have some non-mutable variable, such as an integer. 好吧,假设我们有一些非可变变量,例如整数。 We can do, say, 我们可以说

x = 3
y = x
x = x + 1
print "x =", x, "y =", y

which, as expected, returns: 如预期的那样返回:

x = 4 y = 3

This happens because the line "y = x" creates a copy of the integer x inside of the variable y. 发生这种情况是因为“ y = x”行在变量y内创建了整数x的副本 We can do whatever we want now to x and y doesn't care, because it is not tied to the variable x after its declaration. 我们可以对x和y进行任何我们现在想要的操作,因为它在声明后并不与变量x绑定。

If we do similar operations to lists, we get different results. 如果我们对列表执行类似的操作,则会得到不同的结果。

x = [1,2,3]
y = x
x.append(4)
print "x =", x, "y =", y

returning 回国

x = [1,2,3,4] y = [1,2,3,4]

So what just happened? 那到底发生了什么? Because lists are mutable, when we let y equal x here, instead of creating a copy of x in y, python makes y point to the same data that x points to. 因为列表是可变的,所以当我们在这里让y等于x时,python会把y指向x指向的相同数据,而不是在y中创建x的副本。 So, when we change x, y appears to have magically changed along with it, which makes sense, as the variable y is referring to the same thing that x does. 因此,当我们更改x时,y似乎也随之发生了神奇的变化,这是有道理的,因为变量y所指的是与x相同的事物。 The connection between x and y with mutable types like lists and dictionaries lasts beyond the line "y = x". x和y之间具有可变类型(例如列表和字典)的连接超出了“ y = x”行。

To counteract this, y = x[:] works, which is essentially the same as doing 为了解决这个问题,y = x [:]起作用,这与

y = [] # y is a brand new list!
for elem in x:
    y.append(elem)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM