user1779467
user1779467

Reputation: 31

How to input actual data into classes

Okay, so I have a module I created and have imported it into my main program. I guess I'm still trying to understand classes and I'm not sure how to actually enter my data into my class. This is in Python 3. Here is the module:

class Movie:
    def __init__(self, Title = '', Ratings = []):
        self.Movie = Title
        self.Ratings = Ratings
        self.TotalRatings = 0
        self.minusFive = 0
        self.minusThree = 0 
        self.one = 0
        self.three = 0
        self.five = 0
        self.ValidRatings = [-5,-3,1,3,5]

    def __str__(self):
        return '{0} has {1} total ratings with an average rating of {3}.'.format(self.title, self.totalRatings, self.AvgRating)
    def __repr__(self):
        return self.__str__()
    def AddRating(self, rating):
        for num in self.Ratings:
            if num in self.ValidRatings:
                self.totalRatings += 1 
            else:
                continue
    def AvgRating(self):
        return sum (self.totalRatings)/len(self.totalRatings)

Below is the beginning of my main program:

import Movie

def ReadFile(FileName):
    F = open(FileName)
    return F.readlines()

File = ReadFile('Ratings.txt') 
File2 = open('Prog5Titles.txt').read()       #open movie titles file

movie1 = Movie("The Avengers", [5,5,5])     #this is just a sample to help me understand

When I run this, I am getting the error message: 'module' object is not callable. Can anyone help me understand how to correctly input my data into this?

Upvotes: 0

Views: 243

Answers (1)

Larry Lustig
Larry Lustig

Reputation: 51000

The problem is that you have a file Movie.py that contains a class named, by coincidence, Movie. If you just import Movie, then the python name Movie refers to the contents of the file Movie.py and to refer to the class tucked away in there, you need to write:

 movie1 = Movie.Movie("The Avengers", [5,5,5])

Alternatively, you can do your import like this:

from Movie import Movie

and now the python name Movie refers to something different — the "thing" (in this case, a class) named Movie inside the file Movie.py.

This would be a little clearer if you named the file containing class Movie with a name different from the class defined inside the file. Maybe movie_stuff.py or something. Consider that you'll have additional classes in the project (Actor, Studio) and those classes will probably be defined in the same file as class Movie.

Upvotes: 3

Related Questions