Learning Python: Classes - Review. Mixed class-properties when 2 or more classes are involved in the code

Hi everyone, I’ve tried to finish this review but couldn’t pass over the concept of having 2 or more classes. Here is my code, afterwards I’ll explain my output problem.

class Student:

    def __init__(self, name, year):
        self.grades = []
        self.name = name
        self.year = year
    def __repr__(self):
        return '{name}, age {year}. '.format(name = self.name, year = self.year)
    def add_grade(self, grade):
        if type(grade) == Grade():
            self.grades.append(grade)
    def print_grades(self):
        print(self.grades)

class Grade:
    minimum_passing = 65

    def __init__(self, score):
        self.score = score

So, the problem is that whenever I call the .add_grade method, which is exclusively related to Student() class,

roger_score = Grade(100)
roger.add_grade(roger_score)

it shows up

if type(grade) == Grade(): TypeError: __init__() missing 1 required positional argument: 'score'.

I clearly have mixed things up here, but I can’t get it to see it by myself. Could someone help me tell what’s on here that is happening? Maybe because I called Grades() class in Student() class? What has the score variable from Grades() has to do with the Student() method?

Read that error carefully and it explains the issue, what happens when you use Grade()?

You’ll want to be using isinstance instead of type if this is the kind of chekcing you’ll be doing-
https://docs.python.org/3/library/functions.html#isinstance

1 Like

Because you are using if type(grade) == **Grade()**:

Your program is expecting you to satisfy the score parameter that you defined in the __init__ method of your Grade class.

What if we used Grade instead of Grade() in your if statement? What would you be telling your program to compare there then?

When my friends learn class and object concepts I always recommend this youtube channel. Go to youtube and search for “john philip jones python” , look for class and object playlist. It will certainly help you understand.