Manish Kumar
Manish Kumar

Reputation: 10482

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) ?

Upvotes: 1

Views: 69

Answers (1)

cdlane
cdlane

Reputation: 41872

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)

Upvotes: 3

Related Questions