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:

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
return False

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


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.


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.

1 Like

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):

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


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)
1 Like