python lists are modified when new elements are appended while inside a function

Viewed 40

I'm making a python program with pygame 2.0.1 on windows and my python version is 3.8.1 . In my program i'm testing with floating point precise movements of some objects with x and y coordinates and i have an object called Pixel in a chess-like grid.

In an info display i want to show the current "ideal position" of the Pixel object being the coordinates of the object in float type (which have to be aproxcimated in an int object to get the real position) and the previous ideal position. However whatever i try i cannot get the previous ideal position.

My current trial is making a list with every ideal position that the Pixel passes by so i can get the previous with list[-2]. However it prints the current position instead of the previous one and to find out why, i tried to print the list in every update loop. Doing so i found out a really weird thing being that when i append an element to the list it actually does it but the old elements get converted in a copy of the new element somehow?? Basically i get something like this (every print is a new update loop):

[ [pos0x, pos0y] ]

[ [pos1x, pos1y], [pos1x, pos1y] ]                           #cache_list.append(ideal_pos1)

[ [pos2x, pos2y], [pos2x, pos2y], [pos2x, pos2y] ]           #cache_list.append(ideal_pos2)

where the number is the position in a a specific frame.

My update fuction looks like:

def update():
    global cache_list


    #*various object updates*


    selected_pixel = get_pixel(coords, upper_grid)  #every frame the pixel is selected from the grid

    if selected_pixel.ideal_pos != cache_list[-1]:  #only updates the cache when the pixel moves

        cache_list.append(selected_pixel.ideal_pos)  #HERE THE POSITION IS APPENDED
        print(cache_list, '\n'*2)
    
    prev_idealpos_text = "previous ideal position:  "+str(selected_pixel_pos_list[-2][0])+" / "+str(selected_pixel_pos_list[-2][1])  #this is the text to be displayed


    #...

So yeah what i need to know is just why all the elements of the list change into the new one that i append.


EDIT: Without even touching the code the print it gives me now looks like:

[ [pos0x, pos0y], [pos0x, pos0y] ]

[ [pos1x, pos1y], [pos1x, pos1y] ]

[ [pos2x, pos2y], [pos2x, pos2y] ]

I also changed the code after this to have a look at the cache_list before and after the modification and the modification itself every update cycle, and i saw that even before modifying the cache_list it gets changed in the update loop like a so:

[ [pos0x, pos0y], [pos0x, pos0y] ]                 #print previous list

[pos0x, pos0y]                                     #print list to append

[ [pos0x, pos0y], [pos0x, pos0y] ]                 #print new list

#THOSE 2 SHOULD BE EQUAL ^ v

[ [pos1x, pos1y], [pos1x, pos1y] ]                 #print previous list

[pos1x, pos1y]                                     #print list to append

[ [pos1x, pos1y], [pos1x, pos1y] ]                 #print new list

Also i want to note that no operations are done on the cache_list before the update loops other thn creating it and initialising it with the coords of the Pixel in frame 0.

1 Answers

selected_pixel.ideal_pos is a reference to a list. Actually, you are appending a reference to a list object to the list, but you are not creating a new object. You need to append a copy of the list that stores the position:

cache_list.append(selected_pixel.ideal_pos)

cache_list.append(selected_pixel.ideal_pos[:])
Related