Python prints object address instead of values
It looks like you are attempting to print the array, and not a value in the array. print(self.tracks) is printing the self.tracks object, which is an array. Try print(self.tracks[x]), x being the index of the string you want to print.
If you want to print all of the objects in that array, iterate through it and print each object.
Use this to iterate through the array:
for x in range(len(self.tracks)):
print self.tracks[x].title
or
for track in self.tracks
print track.title
To get the value of the title of each song object, address it in the loop with track.title. To get the artist or year, change it to track.artist or track.year.
You can build larger strings using the same logic, for example: print("Title " + track.title + ", Artist " + track.artist)
Related videos on Youtube
Llama
Java "I cant change the laws of physics. I've got to have 30 minutes."
Updated on June 04, 2022Comments
-
Llama almost 2 years
I want to print all of the tracks that have been added to the
tracks[]
list. When I attempt to do so, I get the address where that object sits in memory, rather than its actual value. I obviously don't understand how object creation/passing of objects from one class to another class works.class Song: def __init__(self, title, artist, album, track_number): self.title = title self.artist = artist self.album = album self.track_number = track_number artist.add_song(self) class Album: def __init__(self, title, artist, year): self.title = title self.artist = artist self.year = year self.tracks = [] artist.add_album(self) def add_track(self, title, artist=None): if artist is None: artist = self.artist track_number = len(self.tracks) song = Song(title, artist, self, track_number) self.tracks.append(song) print(self.tracks) class Artist: def __init__(self, name): self.name = name self.albums = [] self.songs = [] def add_album(self, album): self.albums.append(album) def add_song(self, song): self.songs.append(song) class Playlist: def __init__(self, name): self.name = name self.songs = [] def add_song(self, song): self.songs.append(song) band = Artist("Bob's Awesome Band") album = Album("Bob's First Single", band, 2013) album.add_track("A Ballad about Cheese") album.add_track("A Ballad about Cheese (dance remix)") album.add_track("A Third Song to Use Up the Rest of the Space") playlist = Playlist("My Favourite Songs") for song in album.tracks: playlist.add_song(song)
-
Llama almost 6 yearsok so i am attempting to print the object itself? rather than the attributes of the object? which contain the values I'm looking for?
-
Llama almost 6 yearsi changed it to "for i in range(len(self.tracks)): print(self.tracks[i])" am still getting the object addresses i.e. <__main__.Song object at 0x1082bd6a0> <__main__.Song object at 0x1082bd6a0> <__main__.Song object at 0x1082bd6d8> <__main__.Song object at 0x1082bd6a0> <__main__.Song object at 0x1082bd6d8> <__main__.Song object at 0x1082bd710>
-
Markus Bawidamann almost 3 yearsHuh? Since when can't you print a simple array? I remember being able to get an output of the whole array with all values without having to go through them one by one.