Introduction to Classes Python Review Question #14

Exercise link - https://www.codecademy.com/courses/learn-python-3/lessons/data-types/exercises/review

Prompt:
Create a new Grade with a score of 100 and add it to pieter ‘s .grades attribute using .add_grade() .

I had no problem working my way up the exercises until I got to this point.
It may be a silly question to ask, but how does,

pieter.add_grade(Grade(100))

create an instance of a new score while adding it to pieter’s .grades attribute?

I’m fairly new to python so, any help is much appreciated.

if too much is happening on one line you can break it down into steps:

grade = Grade(100)
pieter.add_grade(grade)

first we create an instance/object of the Grade class, then when use the add_grade method:

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

which will add the grade to the .grades attribute/property

1 Like

Thank you! This makes a lot more sense to me.

why? the grade intermediate variable doesn’t serve much purpose, I would actual have used pieter.add_grade(Grade(100))

You learn a process by breaking it down into steps that are easier to understand. In linked lists it kinda blows my mind that you have all these node objects whose only name reference seems to be the linked list object, and that only knows the first Node.

1 Like

You’re exactly right. This is the way I am more easily able to comprehend syntax. By breaking it down then eventually becoming efficient.