0

My loop at the bottom of my code doesn't seem to print song title if I type print SongList[i] it prints the memory address for the given object. I'm sure it's returning the Song List but the object attributes. Am I missing a method? Sorry if this has already been answered in the past I cannot find an example.

class Song:

    def __init__(self, title, artist, duration = 0):
        self.title = title
        self.artist = artist
        self.duration = duration


class Album:

    def __init__(self, albumTitle, releaseYear, albumSize):
        self.albumTitle = albumTitle
        self.releaseYear = releaseYear
        self.trackList = []
        self.albumSize = albumSize

    def addSong(self, albumSong, position):
        self.trackList.append((position, albumSong))


class Artist:

    def __init__(self, name, year):
        self.name = name
        self.year = year
        self.members = []

    def addMembers(self, bandMember):
        self.members.append(bandMember)


class Person:

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender


BlinkAndSee = Album("Blink and See", 2018, 5)

Band = Artist("Dinkers", 2002)

AlexThomas = Person("Alex Thomas", 23, "Female")
SeanJohnson = Person("Sean Johnson", 25, "Male")
SineadMcAdams = Person("Sinead McAdams", 21, "Female")

Band.members.append(AlexThomas)
Band.members.append(SeanJohnson)
Band.members.append(SineadMcAdams)


Stoner = Song("Stoner", Band, 320)
Blink = Song("Blink and See", Band, 280)
See = Song("See", Band, 291)
DumbnessAndSand = Song("Dumbness and Sand", Band, 231)
OrangeYellow = Song("Orange Yellow", Band, 353)

BlinkAndSee.trackList.append(Stoner)
BlinkAndSee.trackList.append(BlinkAndSee)
BlinkAndSee.trackList.append(See)
BlinkAndSee.trackList.append(DumbnessAndSand)
BlinkAndSee.trackList.append(OrangeYellow)

SongList = BlinkAndSee.trackList

#Loop through the Song list from album tracklist
for i in range(SongList.__len__()):
    print(SongList[i].title)
  • Why do you have an `addSong` method in your `Album` object but then manually use `.tracklist.append(...)` to add songs to the tracklist instead? – AJC24 Jul 09 '18 at 07:49
  • I can't reproduce your problem. It prints `Stoner` for me and then crashes because you added the Album itself to its list. – Arne Jul 09 '18 at 07:51
  • And if i change `BlinkAndSee.trackList.append(BlinkAndSee)` to `BlinkAndSee.trackList.append(Blink)` it prints the songtitles, as you described it should. – Arne Jul 09 '18 at 07:53
  • Always a typo! Thanks for your help – OrangeSubmarine121 Jul 09 '18 at 07:58

1 Answers1

0

This is caused by (probably) a typo: Note how you append BlinkAndSee.trackList.append(BlinkAndSee)?
This essentially appends the object itself to the track list; of course, this will cause an error, since an Artist does not have the attribute title that you are accessing further down when iterating over the list.

In that case, I would suggest you add some lines to your Album.addSong() function that verifies that the passed argument is of a certain type; Something like

def addSong(self, albumSong, position):
    if not isinstance(albumSong, Song):
       print("Passed wrong type to addSong")
    else:
       self.trackList.append((position, albumSong))
dennlinger
  • 9,890
  • 1
  • 42
  • 63