Class Encapsulation Errors in Python 2.3.3

Shalabh Chaturvedi shalabh at cafepy.com
Thu Nov 18 23:37:00 EST 2004


Tim Henderson wrote:

<snip>
> 
> 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   | 
> ************************************************
> 
<snip>

Defnition of addSong() would help in determining the real cause. Taking 
a guess here but it looks like the 'list' you store the songs in is 
shared across all albums. Typically this happens if you define the list 
in the class itself making it a class attribute - and not in __init__(). 
Please show us the code of Album to help you further.

Shalabh




More information about the Python-list mailing list