How can i see the list not its index in a class?

This is the example:

class Student:
  def __init__(self, name, year):
    self.grades = [  ]
    self.name = name
    self.year = year
  def add_grade(self, grade):
    if (type(grade) ==Grade):
      self.grades.append(grade)
instance = Student(anynumber)

If i type( print(instance.grades))
It shows this: [<main.Grade object at 0x7fc66932b6d8&gt>]
While i’m trying to see the GRADES LIST.

calling function and methods requires parentheses:

functionName();

or:

instance.methodName();

Also think about parameters and arguments

1 Like

I wanted to see the grades attribute not the function. The list that i kept on appending grades to.

@r905,

Several parts of your code are missing from what you posted, and we need to see some of that material in order to discuss the problem effectively. In particular, it would be helpful for us to see how you defined the Grades class. Evidently, the definition of the class does not include a __repr__ method. Such a method, if included, would enable this statement to display the individual items within the instance.grades object more informatively:

print(instance.grades)

As you have it now, the individual Grade objects get displayed in a default format within the square brackets.

1 Like

I thought it wouldn’t matter if i shared all the code. But if you’re willing to give it your time, I will appreciate it much. Here is the full code:

class Student:
  def __init__(self, name, year):
    self.grades = []
    self.name = name
    self.year = year
  def add_grade(self, grade):
    if (type(grade) ==Grade):
      self.grades.append(grade)
roger = Student("Roger van der Weyden", 10)
sandro = Student('Sandro Botticelli', 12)
pieter = Student('Pieter Bruegel the Elder', 8)
class Grade:
  minimum_passing = 65
  def __init__(self, score):
    self.score = score
score = Grade(100)
pieter.add_grade(score)
print(pieter.grades)

#out put shows this for the print(pieter.grades): [<main.Grade object at 0x7f0c237876a0>] not the list itself.

The list that you are outputting contains a Grade object. The problem is that the Python interpreter does not know how to output an instance of the Grade class in an informative manner. You can provide a means for it to do that by defining a __repr__ method for that class. It must return a str.

The following revision of your code includes an example:

class Grade:
  minimum_passing = 65
  def __init__(self, score):
    self.score = score
  def __repr__(self):
    # how to represent a Grade instance as a str
    return str(self.score)

class Student:
  def __init__(self, name, year):
    self.grades = []
    self.name = name
    self.year = year
  def add_grade(self, grade):
    if (type(grade) ==Grade):
      self.grades.append(grade)
      
roger = Student("Roger van der Weyden", 10)
sandro = Student('Sandro Botticelli', 12)
pieter = Student('Pieter Bruegel the Elder', 8)

score1 = Grade(94)
score2 = Grade(100)
score3 = Grade(92)
pieter.add_grade(score1)
pieter.add_grade(score2)
pieter.add_grade(score3)
print(pieter.grades)

Output:

[94, 100, 92]
1 Like

Thanks man. I was really anxious about this. Since when i print(type(pieter.graders) it says its a list. Now, I know how. Again, thank you. Just started these classes and inheritance. It feels like a ■■■■ big ocean. But, i’ll figure how to swim.

1 Like