Explain the meaning of type(grade)==Grade?

Hi, can someone explain the meaning of type(grade)==Grade ?
I don’t understand the purpose of this line. Thanks

class Student:
def init(self, name, year):
self.name = name
self.year = year
self.grades =

def add_grade(self, grade):
if type(grade) == Grade:
self.grades.append(grade)

class Grade:

def init(self, score):
self.score = score
def repr(self):
return str(self.score)

def is_passing(self,score):
if self.score>=65:
return True
else:
return False

roger = Student(“Roger van der Weyden”, 10)
sandro = Student(“Sandro Botticelli”, 12)
pieter = Student(“Pieter Bruegel the Elder”, 8)
pieter.add_grade(Grade(100))

pieter_score=Grade(100)
print(pieter.grades)

It means that the value given must be an instance of the Grade class.

To add a grade to the Student instance, we must pass a new Grade instance.

peiter.add_grade(Grade(90))

The type of that value will be <class 'Grade'> since it is typed by its nearest (parent) class definition, of which it is an instance.

Thanks. But I’m curious about why need this line if type(grade) == Grade:, since down below will work too.
def add_grade(self, grade):
self.grades.append(grade)

… is not a Grade object. That object will have a 'score' attribute.

1 Like

Thank you so much!
Another question, how can i print the element inside the grades list?

The only thing that is printable is the score attribute. Access the grade and then, knowing it is a Grade object, its score attribute…

for grade in grades:
    print (grade.score)