Unable to run a simple python program

I started learning python. Here is a simple program:

class StudentRepo:
    def __init__(self):
        self.student_list = []

    def add(self, student):
        self.student_list.append(student)

    def get_list(self):
        self.student_list




class Student:
    def __init__(self, name, age):
        self.age = age
        self.name = name




from models.student.Student import Student
from services.student.StudentRepo import StudentRepo

s1 = Student("A", 10)
s2 = Student("B", 11)

# What is the issue here ?
StudentRepo.add(s1)
StudentRepo.add(s2)

studentList = StudentRepo.get_list()
for student in studentList:
    print(student.name)

What is the issue with s1 = Student("A", 10) ?

1 answer

  • answered 2018-04-17 04:22 cdlane

    There are two mistakes in your code. First, this:

    def get_list(self):
        self.student_list
    

    should be:

    def get_list(self):
        return self.student_list
    

    Second, you're using the class StudentRepo where you should be using an instance of StudentRepo:

    s1 = Student("A", 10)
    s2 = Student("B", 11)
    
    my_roster = StudentRepo()
    
    my_roster.add(s1)
    my_roster.add(s2)
    
    studentList = my_roster.get_list()
    for student in studentList:
        print(student.name)