Idan Perry
Idan Perry

Reputation: 13

Initializing python object using different object types as arguments

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:

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

Upvotes: 1

Views: 1453

Answers (1)

mama
mama

Reputation: 2227

You can use the classmethods as secondary constructors - where you will use the values from the second parameter in the function to fill out the attributes in your Movie class's constructor.

I have made the example using dataclasses since that makes the code shorter, with the same functionality. (or more)

from dataclasses import dataclass

@dataclass
class Content:
    title: str
    url: str

@dataclass
class Data:
    title: str
    year: str
    rating: float

@dataclass
class Movie:
    title: str
    year: str
    rating: float
    url: str

    @classmethod
    def by_content(cls, content: Content): return cls(
        title = content.title,
        url = content.url,
        rating = 0,
        year = 0,
    )

    @classmethod
    def by_content(cls, data: Data): return cls(
        title = data.title,
        url = 'https://null.com',
        rating = data.rating,
        year = data.year,
    )

Upvotes: 2

Related Questions