patternpythonMinor
Music track class
Viewed 0 times
musicclasstrack
Problem
This is my first time working with classes and 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).
object (see below)
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")Solution
In general, looks great! A few things:
Here is your same code with a few of those adjustments. In general though, looking good!
- This relates to 'new-style' classes in Python - make sure that they inherit from
object. It sounds like you are learning about classes now, so I will spare an explanation of what inheritance is since you'll eventually get to it.
- I could be misguided in saying this, but generally I've seen/done type conversion (where you call
str()) outside of the constructor (i.e. the__init__()function). The instructions says it expects a string, and you are already passing a string in theTrack =line, so removing thestr()parts will still result in the correct answer.
- The instructions say that
albumis an "Album object", which means thatAlbumwill be a class as well. I'm assuming you're just getting this started so it hasn't been created yet, but when you do, you can just pass theAlbumobject into yourTrackconstructor.
- Naming - in general, avoid naming your instances the same thing as the class (in your case,
Trackis the class). This leads to confusion, especially when you need to create a second instance (becauseTrackwill then an instance of Track, instead of theTrackclass.
Here is your same code with a few of those adjustments. In general though, looking good!
class Track(object):
def __init__(self, artist, title, album=None):
self.artist = artist
self.title = title
self.album = album
def __str__(self):
# This is called 'string formatting' and lets you create a template string
# and then plug in variables, like so
return "%s - %s (%s)" % (self.artist, self.title, self.album)
def set_album(self, album):
self.album = album
my_track = Track("Andy", "Me", "Self named")
# This will show you what __str__ does
print my_trackCode Snippets
class Track(object):
def __init__(self, artist, title, album=None):
self.artist = artist
self.title = title
self.album = album
def __str__(self):
# This is called 'string formatting' and lets you create a template string
# and then plug in variables, like so
return "%s - %s (%s)" % (self.artist, self.title, self.album)
def set_album(self, album):
self.album = album
my_track = Track("Andy", "Me", "Self named")
# This will show you what __str__ does
print my_trackContext
StackExchange Code Review Q#16332, answer score: 4
Revisions (0)
No revisions yet.