0

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?

Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • 1
    Aside: the line `def __init__(self, Title = '', Ratings = []):` probably doesn't do what you think it does. Instead of making a new empty `Ratings` list every time `__init__` is called without one being passed, it makes exactly *one*, when the function is defined. Please read [this question](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) on how mutable default arguments work. – DSM Nov 03 '12 at 02:35

1 Answers1

3

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.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • Okay, well I got that all figured out, but now I still am not sure how to go about the problem. Here is a link to my new question if anyone wants to visit it and help me out... – user1779467 Nov 04 '12 at 23:46