Class Encapsulation Errors in Python 2.3.3

Tim Henderson tim.tadh at gmail.com
Thu Nov 18 23:07:55 EST 2004


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