In the get_average
method, self.scores
is just a temporary list variable in this case, used to sum up all the scores.
class Student:
def __init__(self, name, year):
self.name = name
self.year = year
# list of Grade objects, not scores!
self.grades = []
def add_grade(self, grade):
if type(grade) is Grade:
self.grades += [grade]
def get_average(self):
self.scores = [item.score for item in self.grades]
return sum(self.scores) / len(self.scores)
class Grade:
minimum_passing = 65
def __init__(self, score):
self.score = score
But if you dont need to access this value with respect to each instance (e.g. you don’t need Pieter’s list of scores) can you instantiate the variable without the self prefix, like this? If not, what are the reasons to avoid this?
def get_average(self):
scores = [item.score for item in self.grades]
return sum(scores) / len(scores)
https://www.codecademy.com/courses/learn-python-3/lessons/data-types/exercises/review