Class Encapsulation Errors in Python 2.3.3
Terry Reedy
tjreedy at udel.edu
Fri Nov 19 16:32:48 EST 2004
"Tim Henderson" <tim.tadh at gmail.com> wrote in message
news:47f7cc78.0411191005.7b793bf at posting.google.com...
> 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 = ''
>
> self.name = name
> self.artist = artist
add
self.songs = []
now songs is an attribute of the album.
> possible revision
[snip]
> 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