I am quite new to OOP with Python, and trying to understand how the __repr__
method should output in this particular instance.
When initialising the Playlist class, an array, containing song objects, can be passed as an argument. You can also pass nothing as an argument, and add songs objects to a playlist using a separate add_song
method.
However I'm not quite sure how to set up my __repr__
method in this case. If I pass an array of 10+ song objects, the output of repr method would be extremely large - as each song object itself has several parameters.
Is there a Pythonic way of doing this?
EDIT: To make my question less vague - from what I understand, the repr method is intended to return a string representation of an object. In my case, Playlist objects are passed an array as an argument (or nothing at all). If an array is passed, this contains song objects, which themselves have a number of parameters such as name of song, language of song, etc. My question is whether there is a 'correct' way to set up the repr method in this instance - or if this is really just up to me to decide.
class Playlist:
def __init__(self, songs = None):
if songs is None:
self.playlist = []
else:
self.playlist = songs
def __repr__(self):
return (f"{self.__class__.__name__}({self.playlist})")
Output of printing a playlist object:
Playlist([Song(Random Song Name 1, English, 50), Song(Random Song Name 2, English, 25)])
__repr__
in PythonSource
__repr__
should be return a string representation of your object. This is useful for debugging. That means you should return all the attributes of a class in a way you would prefer.Example
From your example, you need to return the names of the songs and their ID's.
This is how I would do it. You are free to choose the output format. I am pretty sure you must have more attributes in this class. You shall return them in this function.
From your question
It is true that if you have a huge list of songs for an object the string representation could be huge. A good way must be to make the string representation more organized and hence easier to read. Something like
The point of this method is that you can call it whenever you want and get all the attributes of the object, Suppose you have an issue regarding the artist being displayed incorrectly. A good start of debugging can be to display the object attributes in an organized manner so you can trace back to exactly where the problem comes from.