This is my first time working with classes, it's still unclear to me. I was wondering if someone can tell me if I did this right..
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
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")
set_albummethod is redundant and unpythonic: simply assign to the attribute directly in your code. If at any point in the future you need some more complicated behaviour when setting the attribute, look into using a property then. – Pedro Romano Oct 9 '12 at 0:54set_album. – poorsod Oct 12 '12 at 21:43