0
from movie import Movie


def run_tests():

    enter code here
    print("Test empty movie:")
    default_movie = Movie()
    print(default_movie)
    assert default_movie.title == ""
    assert default_movie.category == ""
    assert default_movie.year == 0
    assert not default_movie.is_watched

    initial_movie = Movie("Thor: Ragnarok", 2017, "Comedy", True)


run_tests()

Is it possible to create a class that can be instantiated like this? I've came across and I don't know the solution to this. The class needs to have an init and str constructors

This is what I came so far

class Movie:
    def __init__(self, title, year, category, is_watched):
        self.title = title
        self.year = year
        self.category = category
        self.is_watched = is_watched
    def __str__(self):
        return 'movie=> {} {} {} {} {}'.format(self.name, self.year, self.genre, self.is_watched)

#sw = Movie("SW", 1977, "action", 0);
#print(sw);

But when I run the test file regarding this class, a error is thrown

TypeError: init() missing 4 required positional arguments: 'title', 'year', 'category', and 'is_watched'

2
  • Possible XY problem: why do you need to create a movie that doesn't have any details about that movie initially? Commented Jan 23, 2020 at 18:12
  • If there is a reason to create an empty movie, I would do so as an explicit special case, rather than as a default action. Define a class method that returns Movie("", 0, "", False). Commented Jan 23, 2020 at 18:18

1 Answer 1

2

Quite simply yes:

 def __init__(self, title="", year=0, category="", is_watched=False):

May I kindly suggest you do the full official Python tutorial ? This would have answered your question...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.