I'm writing a program that allows a user to search an API for media information and then divided them into Songs and Movies. I'm using classes to do this and have a parent class (Media) and two child classes (Song, Movie). My Media class works great when I search the API and I get all the information I want. However, when I try to create Song or Movie instances, the information won't inherit from the Media class (I get an error that the subclass is missing positional arguments) and I can't figure out why. Any help would be great!
class Media:
def __init__(self, title="No Title", author="No Author", release_year="No Release Year", url="No URL", json=None):
if json==None:
self.title = title
self.author = author
self.release_year = release_year
self.url = url
else:
if 'trackName' in json.keys():
self.title = json['trackName']
else:
self.title = json['collectionName']
self.author = json['artistName']
self.release_year = json['releaseDate'][:4]
if 'trackViewUrl' in json.keys():
self.url = json['trackViewUrl']
elif 'collectionViewUrl' in json.keys():
self.url = json['collectionViewUrl']
else:
self.url = None
def info(self):
return f"{self.title} by {self.author} ({self.release_year})"
def length(self):
return 0
class Song(Media):
def __init__(self, t, a, r_y, u, json=None, album="No Album", genre="No Genre", track_length=0):
super().__init__(t,a, r_y, u)
if json==None:
self.album = album
self.genre = genre
self.track_length = track_length
else:
self.album = json['collectionName']
self.genre = json['primaryGenreName']
self.track_length = json['trackTimeMillis']
def info(self):
return f"{self.title} by {self.author} ({self.release_year}) [{self.genre}]"
def length(self):
length_in_secs = self.track_length / 1000
return round(length_in_secs)
class Movie(Media):
def __init__(self, t, a, r_y, u, json=None, rating="No Rating", movie_length=0):
super().__init__(t, a, r_y, u)
if json==None:
self.rating = rating
self.movie_length = movie_length
else:
self.rating = json['contentAdvisoryRating']
self.movie_length = json['trackTimeMillis']
def info(self):
return f"{self.title} by {self.author} ({self.release_year}) [{self.rating}]"
def length(self):
length_in_mins = self.movie_length / 60000
return round(length_in_mins)
super().__init__(t,a, r_y, u)withMedia.__init__(self,t,a,r_y,u)Movie) suggests, you have to provide explicitly the positional args namely,t,aetc. while creating an instance of the same.