Class Encapsulation Errors in Python 2.3.3
Tim Henderson
tim.tadh at gmail.com
Fri Nov 19 13:05:31 EST 2004
Here is a partial implementation, i don't have the code with me but
this is how the songs list is made
code:
-------------------------------------------
class Album:
name = ''
songs = []
artist = ''
def __init__(self, name, artist):
self.name = name
self.artist = artist
-------------------------------------------
after reading the code i came up with a possible solution:
possible code:
-------------------------------------------
class Album:
name = ''
songs = False
artist = ''
def __init__(self, name, artist):
self.name = name
self.artist = artist
if songs == False:
songs = []
------------------------------------------
i will test this when i get home, and update the thread with results
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