0

when I add s1 object in p1 it is being automatically added in p2 too. How can I fix it? i want s1 to be only added in p1 and s2 to be only added in p2 because later on i need to union p1 and p2 as "merged_playlist" that will contain both p1 and p2 songs, so when s1 and s2 are both added in p1 and p2 i get a different result

class Song:
    def __init__(self, song_name, album_name, artist, length):
        self.song_name = song_name
        self.album_name = album_name
        self.artist = artist
        self.length = length

    def __str__(self):
        return f'Music name: {self.song_name}, Album name: {self.album_name}, Artist: {self.artist}, Duration: {self.length}'


class Playlist:
    def __init__(self, album_name, music_list=[]):
        self.album_name = album_name
        self.music_list = music_list

    def __str__(self):
        return f'Album name: {self.album_name}, Musics: {self.music_list}'

    def add_song(self, song):
        if isinstance(song, Song):
            self.music_list.append(song.song_name)

    def __add__(self, other):
        if isinstance(other, Playlist):
            for musics in other.music_list:
                self.music_list.append(musics)
                return Playlist("merged_playlist", self.music_list)


s1 = Song("Zaris Xmaze Gamegvidza", "Qartuli", "Ucnobi", 200)
s2 = Song("All for you", "Ucxouri", "Unknown", 150)

p1 = Playlist("Qartuli Musikebi")
p2 = Playlist("Ucxouri Musikebi")
p1.add_song(s1)
p2.add_song(s2)
merged = p1 + p2

0 Answers0