What is the mean of code if type(grade) == Grade

I get some trouble to understand what is the mean of code if type(grade) == Grade: its return true? I so appreciate that anyone wants to answer my question. Thank you

its a condition to check grade variables holds an instance of Grade class

Emm. I still can’t reach the point. why we must use this if type(grade) == Grade:? using this roger.add_grade(Grade(68)) Why don’t we just append the grade variables? using this roger.add_grade(68)

You have two different questions.

The condition is merely to validate the parameter is the right type

We could, but I would like to take a different angle here. The exercise gives us a problem, Grade class doesn’t do much. It would have been much better if Grade class had more functionality/complexity to justify the need for this abstraction

This is always a dilemma, introducing a new concept (validating parameter of certain type in this case) is useful, but you don’t want to make things to complex when introducing a new concept, as a result, its difficult to see the true potential of this concept

1 Like

Oh okay. I see, thanks for the explanation. :grin:

Actually, in a lot of languages we can do something like this:

def add_grade(self, Grade grade):

where can specify a static type, any other types are not accepted. Python even has type annotation:

https://medium.com/analytics-vidhya/type-annotations-in-python-3-8-3b401384403d

The more I program, the more I prefer static typing or at the very least type hinting/annotation. So you know what data types you are dealing (which is very useful once your project starts to grow in size)

2 Likes