5
\$\begingroup\$

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).

  1. 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)
  2. Has a method __str__(self) that returns a reasonable string representation of this track
  3. 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")
\$\endgroup\$
2
  • 3
    \$\begingroup\$ The set_album method 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. \$\endgroup\$ Oct 9 '12 at 0:54
  • \$\begingroup\$ @PedroRomano - agreed, but it seems as though the homework question (I assume it's homework?) is explicitly asking for the method set_album. \$\endgroup\$ Oct 12 '12 at 21:43
4
\$\begingroup\$

In general, looks great! A few things:

  1. 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.
  2. 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.
  3. 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.
  4. 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
\$\endgroup\$
2
  • \$\begingroup\$ Regarding point 1: you don't need to inherit from object in Python 3.x. I can't tell what version he's using. Also, the % formatting operator has been superseded by string.format() (though of course the old style is still hugely common). \$\endgroup\$ Oct 12 '12 at 21:44
  • 2
    \$\begingroup\$ @poorsod Agreed - I actually don't ever use the % operator :) Figured it would be the easier one to explain, but am looking forward to a .format()-only future! As to inheriting from object, I blindly assumed that he was using 2.x - I need to get with the future I guess :) \$\endgroup\$ Oct 12 '12 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.