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:
{}creates a dictionary, not a list.ListOfPupilscreates a local variable rather than an instance attribute. In short, it would be more beneficial for you to read a tutorial about Python classes.