I'm told to
Define a new class, Track, that has an artist (a string), a title (also a string), and an album (see below).
- Has a method
__init__(self, artist, title, album=None)
. The arguments artist and and title are strings and album is an Album object (see below)- Has a method
__str__(self)
that returns a reasonable string representation of this track- Has a method
set_album(self, album)
that sets this track's album to album
This is my first time working with classes, and I was wondering if someone can explain the difference between using strings and objects in Python. I read up about __str__
also, but I'm not exactly sure how it works. It says the "the string returned by __str__
is meant for the user of an application to see" but I never see the return value for my inputs. Can someone explain the use of __str__
please?
I'm not sure if I followed the guideline correctly either, if someone could confirm what I did is correct, that would be great.
class Track:
def __init__(self, artist, title, album=None):
self.artist = str(artist)
self.title = str(title)
self.album = album
def __str__(self):
return self.artist + " " + self.title + " " + self.album
def set_album(self, album):
self.album = album
Track = Track("Andy", "Me", "Self named")