简体   繁体   English

当它是列表,整型或字符串时,为什么输出不相同?

[英]Why are the outputs not the same when it is either a list, int or string?

Some code copied (and altered) from my pygame game. 从我的pygame游戏复制(并更改)了一些代码。 But the same problem occurs that i do not understand. 但是发生同样的问题,我不明白。 Why is the background_pos_init output different when it is a list, and when it is an integer or string? 为什么background_pos_init是列表,整数或字符串时输出为何不同?

class Player():
    def __init__(self):
        self.pos_init = [16]
        self.pos = self.pos_init


class Window():
    def __init__(self):
        self.background_pos_init = [0]
        self.background_pos = self.background_pos_init

    def moveBackground(self):
        self.background_pos[0] = self.background_pos_init[0] + player.pos[0]

        print(self.background_pos)
        print(self.background_pos_init)


player = Player()
window = Window()

window.moveBackground()

gives the output: 给出输出:

============== RESTART: C:\Users\SAMP\Documents\Python\test.py ==============
[16]
[16]
>>> 

and

class Player():
    def __init__(self):
        self.pos_init = 16
        self.pos = self.pos_init


class Window():
    def __init__(self):
        self.background_pos_init = 0
        self.background_pos = self.background_pos_init

    def moveBackground(self):
        self.background_pos = self.background_pos_init + player.pos

        print(self.background_pos)
        print(self.background_pos_init)


player = Player()
window = Window()

window.moveBackground()

gives the output: 给出输出:

============== RESTART: C:\Users\SAMP\Documents\Python\test.py ==============
16
0

And if i change the values to 'str', the output is: 如果我将值更改为“ str”,则输出为:

============== RESTART: C:\Users\SAMP\Documents\Python\test.py ==============
strstr
str

That's because, when you declare you variable as list, it stores the reference (memory address) of the object. 这是因为,当您将变量声明为list时,它将存储对象的引用(内存地址)。 When you access the variable by index , both the objects update the value of the same list object. 当您通过index访问变量时,两个对象都会更新同一列表对象的值。 But that's not the case when you initialize without index. 但是,当您不使用索引进行初始化时,情况并非如此。 In that case a new object is initialized to the variable. 在这种情况下,将新对象初始化为变量。

Hence in your example one: 因此,在您的示例中:

self.background_pos_init = [0]
self.background_pos = self.background_pos_init

Both self.background_pos_init and self.background_pos holds the reference for same list. self.background_pos_initself.background_pos保存同一列表的引用。 Hence when you change the value within one list, it will be reflected in both the variables holding it. 因此,当您在一个列表中更改值时,它将反映在保存该值的两个变量中。

But that is not true in the example 2: 但这在示例2中是不正确的:

self.background_pos_init = 0
self.background_pos = self.background_pos_init

Both self.background_pos_init and self.background_pos holds value as 0. When you update the value, let's say x = 0 , a new object with value 0 is mapped to the object x, and your other variable will still still hold the value of the older object. 无论self.background_pos_initself.background_pos当您更新的价值,假设持有价值为0 x = 0 ,有值的新对象0被映射到对象x,和您的其他变量仍然会仍持有的价值较旧的对象。

It's a little unclear what you're asking, but I think it's because you don't actually see how the variables relate. 不清楚您要问的是什么,但是我认为这是因为您实际上看不到变量之间的关系。

You have three statements going on here: 您在这里有三个语句:

self.pos_init = 16
self.background_pos_init = 0
self.background_pos = self.background_pos_init + player.pos

Let's simplify this. 让我们简化一下。 Consider the following: 考虑以下:

pos_init = 16
background_pos_init = 0
background_pos = background_pos_init + pos_init

Why does print(background_pos) display 16? 为什么print(background_pos)显示16? And if we change the code: 如果我们更改代码:

pos_init = [16]
background_pos_init = [0]
background_pos[0] = 0
background_pos[0] = background_pos_init[0] + pos_init[0]
print(background_pos)

Why does this display [16] ? 为什么显示[16]

And if you have: 如果您有:

pos_init = 'str'
background_pos_init = 'str'
background_pos = background_pos_init + pos_init

Why does print(background_pos) print strstr ? 为什么print(background_pos)打印strstr

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

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