Class Encapsulation Errors in Python 2.3.3

Terry Reedy tjreedy at
Fri Nov 19 22:32:48 CET 2004

"Tim Henderson" <tim.tadh at> wrote in message 
news:47f7cc78.0411191005.7b793bf at
> 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 = ''

Delete all three of these lines.  The name and artist lines have no effect 
since they are over-ridden by the local variables of the same name in 
__init__.  The songs line makes songs an attribute of the Album class, 
which you do not want, instead of each Album instance, which you do.

>    def __init__(self, name, artist):

If you want artist to be optional, augment to ..., artist = ''

> = name
>        self.artist = artist

          self.songs = []
now songs is an attribute of the album.

> possible revision
>        if songs == False:
>            songs = []

This assignment makes songs a local variable in the init function, which is 
again what you don't want.  In fact, the test will fail since local var 
songs will not have a value before the assignment.  See proper fix above.

Terry J. Reedy

More information about the Python-list mailing list