Search code examples
pythonlibvlc

Python -- var getting set without me setting it, I may be losing my mind


This should be so simple, but I just can't see what's going on here:

def playlist_item_added(self, _):
        temp_playlist = self.playlist_olv.GetObjects()
        if not self.mirror_playlist:
            self.mirror_playlist = temp_playlist
            for item in self.mirror_playlist:
                self.vlc_playlist.add_media(u.http_space_escape(item["loc"]))
            self.vlc_player.set_media_list(self.vlc_playlist)
            print "if"
            print self.mirror_playlist
        else:
            print "else"
            print self.mirror_playlist
            for index, item in enumerate(temp_playlist):
                if not item == self.mirror_playlist[index]:
                    media = vlc.Media(u.http_space_escape(item["loc"]))
                    #self.mirror_playlist.insert(index, item)
                    self.vlc_playlist.lock()
                    self.vlc_playlist.insert_media(media, index)
                    self.vlc_playlist.unlock()

The problem is self.mirror_playlist seems to be getting added to without me asking. I just can't see why. This is an event handler, when something gets added to my list, it comes here. It checks if self.mirror_playlist has been set already -- if not, it sets it, no problem. If it is set already, it adds to it (the else block).

Notice the prints I put in. When I add the first item, everything goes as planned. When I add the second item, the print self.mirror_playlist just after the else prints that the second item has already been added to it. Further, I even commented out the part of the code that inserts into that list as you can see in the middle of the else block.

I checked every last line of code and the mirror_playlist attribute isn't mentioned anywhere else except in this function and in the constructor where it sets to None. I double-checked the indents, I double-checked with prints that the flow is exactly how it should be (it is). How is this variable getting added to? I'm almost certain it's something so simple that I can't see it.


Solution

  • When you add the first item, you are not copying the list, change that line to:

    self.mirror_playlist = temp_playlist[:]