HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Music track class

Submitted by: @import:stackexchange-codereview··
0
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).



  • 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:

  • 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 the Track = line, so removing the str() parts will still result in the correct answer.



  • The instructions say that album is an "Album object", which means that Album will 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 the Album object into your Track constructor.



  • Naming - in general, avoid naming your instances the same thing as the class (in your case, Track is the class). This leads to confusion, especially when you need to create a second instance (because Track will then an instance of Track, instead of the Track class.



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_track

Code 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_track

Context

StackExchange Code Review Q#16332, answer score: 4

Revisions (0)

No revisions yet.