I'v read how to overload... and multiple constructors in python and a few more on this topic, however I'm looking for something more specific.
I'm given a list of Content
objects and a list of Data
objects:
class Content:
def __init__(self):
self._title = 'movie title'
self._url = 'http://movie-url'
@property
def title(self):
return self._title
@property
def url(self):
return self._url
class Data:
def __init__(self):
self._title = 'movie title'
self._year = 2021
self._rating = 7.6
@property
def title(self):
return self._title
@property
def year(self):
return self._year
@property
def rating(self):
return self._rating
I want to match each Content
with it's corresponding Data
by the title property and combine everything under one class Movie
, by passing one of the other objects to Movie
's init argument:
movie_content = Movie(Content())
movie_data = Movie(Data())
From what I'v read so far my options are:
- Default arguments: Doesn't seem to fit here (correct me if I'm wrong) since I want to pass only one argument anyway.
*args
: I prefer to avoid passing a long line of arguments (there will be at least 12).@classmethod
: This approach is the most appealing to me, but I'm struggling with the implementation:
class Movie:
def __init__(self, object):
self.object = object
@classmethod
def by_content(cls, content):
_title = content.title
_url = content.url
return cls( # what goes here?)
@classmethod
def by_data(cls, data):
_title = data.title
_year = data.year
_rating = data.rating
return cls( # what goes here?)
- Using methods as multi-setters, which I'm currently using (not the most pythonic from my understanding):
class Movie:
def __init__(self):
self._title = ''
self._url = ''
self._year = 0
self._rating = 0.0
def by_content(self, content):
self._title = content.title
self._url = content.url
def by_data(self, data):
self._title = data.title
self._year = data.year
self._rating = data.rating
Any thoughts or suggestions would be greatly appreciated.