Class Encapsulation Errors in Python 2.3.3

Tim Henderson tim.tadh at gmail.com
Fri Nov 19 23:09:35 CET 2004


I have solved the problem, it was indeed declaring the songs list in
the wrong place. I moved the declaration and the songs list no longer
acts like a static variable. I am going to have to read up on how the
interpreter evaluates variables declared there.


below is the new code, In this code the songs list no longer act like
the same variable.

code: 
------------------------------------------------------------------
class Song:
    
    name = ''
    artist = ''
    album = ''
    
    def __init__(self, name, artist='', album=''):
        
        self.name = name
        self.artist = artist
        self.album = album
    
    
    def setName(self, name):
        self.name = name
    
    def getName(self):
        return self.name
    
    def setArtist(self, artist):
        self.artist = artist
    
    def getArtist(self):
        return self.artist
    
    def setAlbum(self, album):
        self.album = album
    
    def getAlbum(self):
        return self.album
    
    def __str__(self):
        return self.name + '  ' + self.artist + '  ' + self.album + ' 
'


class Album:

    songs = False
    name = ''
    artist = ''
    
    def __init__(self, name, artist=''):
        if not self.songs: self.songs = []
        self.name = name
        self.artist = artist
    
    def setName(self, name):
        
        self.name = name
        for x in songs:
            x.setAlbum(self.name)
    
    def getName(self):
        return self.name
    
    def setArtist(self, artist):
        self.artist = artist
        for x in self.songs:
            x.setArtist(self.artist)
    
    def getArtist(self):
        return self.artist
    
    def addSong(self, song): #must be song class
        #print 'NAME: ' + self.name
        song.setAlbum(self.name)
        song.setArtist(self.artist)
        self.songs.append(song)
    
    def deleteSong(self, name):
        
        x = 0
        while x < self.songs.__len__():
            cname = self.songs[x].getName()
            
            if cname == name: 
                del self.songs[x]
                return True
            
            x += 1
        
        return False
    
    def __str__(self): 
        val = self.name + '\t'
        
        for x in self.songs:
            val += x.__str__() + " | "
        
        return val
    def getSongs(self):
        return self.songs
        
    
    




class Artist:
    
    name = ''
    songs = False #this is only really for deleting songs
    albums = False
    unknownAlbum = '' #set in init
    
    def __init__(self, name):
        if not self.songs: self.songs = []
        if not self.albums: self.albums = []
        self.unknownAlbum = Album('unknown')
        self.albums.append(self.unknownAlbum)
        self.name = name
        self.unknownAlbum.setArtist(self.name)
    
    def setName(self, name):
        for x in albums:
            self.name = name
    
    def getName(self):
        return self.name
    
    def addSong(self, song): #must be song class
##~         print 'NAME:\t:'+self.name
        song.setArtist(self.name)
        
        a = song.getAlbum()
        for x in self.albums:
            if a == x.getName():
##~                 print '--------'
##~                 print a + '  ' + x.getName()
##~                 print x
##~                 print song
##~                 print '--------'
                x.addSong(song)
                print 'ya'
                self.songs.append(song)
                print self.songs
##~                 print '********'
##~                 print x
##~                 print self.unknownAlbum
##~                 print '********'
                return
        
        if a == '': 
            
            self.unknownAlbum.addSong(song)
            #print "yo " + song.__str__()
            a = True
            self.songs.append(song)
            return
            
        elif a != True:
            #print 'yea ' + song.__str__()
            newAlbum = Album(a)
            newAlbum.setArtist(self.name)
            self.albums.append(newAlbum)
            
            print self.addSong(song)
##~             print "@"+
self.albums[self.albums.__len__()-1].__str__()
            return
        
        
    
    def deleteSong(self, name): #give song name
        
        x = 0
        while x < self.songs.__len__():
            cname = self.songs[x].getName()
            
            if cname == name:
                a = self.songs[x].getAlbum()
                
                for calbum in self.albums:
                    if a == calbum.getName(): 
                        calbum.deleteSong(name)
                
                del self.songs[x]
                return True
            
            x += 1
        
        return False
    
    def addAlbum(self, Album): #must be album class
        aSongs = Album.getSongs()
        for song in aSongs: self.songs.append(song)
        Album.setArtist(self.name)
        self.albums.append(Album)
    
    def deleteAlbum(self, name):
        
        x = 0
        while x < self.albums.__len__():
            cname = self.albums[x].getName()
            
            if cname == name:
                
                cSongs = self.albums[x].getSongs()
                
                for cSong in cSongs:
                    self.deleteSong(cSong.getName())
                
                del self.albums[x]
                return True
            
            x += 1
        
        return False
    
    def __str__(self):
        val = ''
        for x in self.albums:
            val += x.__str__() + '\n'
            
        
        return val
    
    def getSongs(self): return self.songs
    def getAlbums(self): return self.albums

#test stuff
s = Song('song 1')
s2 = Song('song 2')

a = Album('hey')
a.addSong(s)

art = Artist('Tim')

art.addAlbum(a)
art.addSong(s2)

print art
art.deleteSong(s.getName())

print art
---------------------------------------------------------------------


cheers
Tim Henderson








"Dan Perl" <danperl at rogers.com> wrote in message news:<nrudnceoVIIV5QDcRVn-hw at rogers.com>...
> Please post the implementation of the Album class.  The problem is coming 
> probably from the way the Songs list is defined or initialized.  That is, 
> the definition or the initialization probably cause the list object that is 
> assigned to Songs to be shared by all the Album instances.  But I (and 
> likely no one else) can say exactly what the problem is without seing the 
> code.
> 
> Dan
> 
> "Tim Henderson" <tim.tadh at gmail.com> wrote in message 
> news:47f7cc78.0411182007.1b77c2b1 at posting.google.com...
> > Hi i have some errors in my code that i can't find the reason for here
> > is the error
> >
> > i have 3 classes:
> > -Song
> > -Album
> > -Artist
> >
> > Song has 3 pieces of data with getter and setter meathods:
> > -Name | i.e. song name
> > -Album
> > -Artist
> >
> > Album has 3 pieces of data:
> > -Songs | a list of Song() instances
> > -Name
> > -Artist
> >
> > Artist has 4 pieces of Data:
> > -Name
> > -Songs | a list of Song() instances
> > -Albums |a list of Album() instances
> > -unknownAlbum | the album a song is put into if it doesn't have an
> > album
> >
> >
> > now the problem arises when i add songs into multiple albums like
> > this:
> > Code:
> > ------------------------------------------------
> > s = Song('song 1')
> > s2 = Song('song 2')
> >
> > a = Album('hey')
> > a.addSong(s)
> > ab = Album('yeayea')
> > print a
> > print ab
> > ab.addSong(s2)
> > print
> > print a
> > print ab
> > ------------------------------------------------
> > Output:
> > ************************************************
> > hey song 1    hey   |
> > yeayea song 1    hey   |
> >
> > hey song 1    hey   | song 2    yeayea   |
> > yeayea song 1    hey   | song 2    yeayea   |
> > ************************************************
> >
> > the "hey" album has "song 1" as it is suppose to however so does
> > "yeayea" even though when "song 1" was put in the album "hey" 'yeayea'
> > didn't even exist yet.
> >
> > Why is this happening. I checked the actually memory refrence for each
> > instance and they are different, these supposidly isolated different
> > intances of clases are linked for some inexplicable reason.
> >
> > Does any one know why this is happening and how i can fix it?
> >
> > cheers
> > Tim Henderson



More information about the Python-list mailing list