Swatcat
Swatcat

Reputation: 47

Going through list from class in python

For a school project I have to write a python program that records student records and display them.

My school said she wanted a class that represents the school register but I don't know understand how I do that.

I created the following:

class SchoolRegister:

    def __init__(self):
        ListOfPupils = {}

    def REgisterPupil(self, lastname, firstname, schoolID):
        ListOfPupils[schoolID] = (lastname, firstname)

 sr = SchoolRegister()
 sr.REgisterPupil("Thomas", "Lucy", 1)
 sr.REgisterPupil("Percival", "Jenny", 2)

I would like to be go through list so I get list out: 1, Thomas, Lucy 2, Percival, Jenny

I searched and found a way of making class act as pupil list thingy, so I can do a for bod in sr but I don't understand it :(

Upvotes: 0

Views: 65

Answers (1)

ospahiu
ospahiu

Reputation: 3525

This problem presents a platform upon which we can discuss some interesting Object Oriented programming concepts! We would ideally like to map the real world with code by creating models of the data that represent the real world. This is the inherit value that Object Oriented programming affords us.

Let's use your example.

Our focus is to be able to create records of students that exist (perhaps we should model a student and create parameters which define what a student means to us) and register (we would ideally like to be able to add and remove students from it and be able to retrieve the state of our student body) them in a central place that we can access.

So we can go ahead and implement a simple Student class that represents a student as far as we are concerned (hard to really quantify human beings on a few factors, so let's narrow our focus :D):

class Student:
    def __init__(self, id, first_name, last_name):
        self.id = id
        self.first_name = first_name
        self.last_name = last_name

We now have the ability to create objects that represent a student and store that information in a structured way. Now moving on to the the school register, let's think about what sort of data we need. How can we have full awareness of all of the students that exist? A likely data structure we could use could be a list! This grants us the option to (1) store all of the student's that exist (2) Maintain the order in which the student were inserted into the list (or perhaps enrolled at the school).

What if we want to be able to quickly access a Student object though? Perhaps we'd like to know more about a student in the register and we have access to their id. Well in that case we could use a dict which affords us easy student lookups using some sort of unique identifer (i.e. an id).

Let's go ahead and implement a simple example of that:

class SchoolRegister:
    def __init__(self):
        self.students = {}

    def register_student(self, student):
        self.students[student.id] = student

    def get_student_by_id(self, id):
        return self.students[id]

Now we can (1) create a Student, (2) add it to our records, (3) lookup that student information with a key (their id) as shown below:

school_register = SchoolRegister()
john_doe = Student(0, 'John', 'Doe')
school_register.register_student(john_doe)
school_register.get_student_by_id(0)  # John Doe!

Suggested Readings:

Upvotes: 2

Related Questions