简体   繁体   中英

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. 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?

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:

============== 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. When you access the variable by index , both the objects update the value of the same list object. 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. 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:

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.

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? 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] ?

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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