Object-Oriented Programming in Python

Pranav S Khodanpur (anushku)
3 min readJan 4, 2024

--

Python, known for its simplicity and versatility, offers powerful tools for implementing object-oriented programming (OOP) concepts. Let’s get into a Python program featuring a School class that exemplifies core OOP principles.

# school.py:
class School:
def __init__(self, name, location, students=None, teachers=None):
self.name = name
self.location = location
self.students = students if students is not None else []
self.teachers = teachers if teachers is not None else []

def add_student(self, student):
self.students.append(student)

def remove_student(self, student):
if student in self.students:
self.students.remove(student)
else:
print(f"{student} is not enrolled in {self.name}.")

def add_teacher(self, teacher):
self.teachers.append(teacher)

def remove_teacher(self, teacher):
if teacher in self.teachers:
self.teachers.remove(teacher)
else:
print(f"{teacher} is not a teacher at {self.name}.")


if __name__ == "__main__":
my_school = School("ABC School", "123 Main St")
my_school.add_student("Alice")
my_school.add_student("Bob")
my_school.add_teacher("Mr. Smith")

print(f"Students in {my_school.name}: {my_school.students}")
print(f"Teachers in {my_school.name}: {my_school.teachers}")

my_school.remove_student("Charlie")
my_school.remove_teacher("Ms. Johnson")

Deconstructing the Code

  • Class Initialization (__init__):
def __init__(self, name, location, students=None, teachers=None):
self.name = name
self.location = location
self.students = students if students is not None else []
self.teachers = teachers if teachers is not None else []

The School class initializes with name, location, students, and teachers.

It assigns name and location attributes and initializes students and teachers as empty lists unless provided during instantiation.

  • Methods:
    def add_student(self, student):
self.students.append(student)

def remove_student(self, student):
if student in self.students:
self.students.remove(student)
else:
print(f"{student} is not enrolled in {self.name}.")

def add_teacher(self, teacher):
self.teachers.append(teacher)

def remove_teacher(self, teacher):
if teacher in self.teachers:
self.teachers.remove(teacher)
else:
print(f"{teacher} is not a teacher at {self.name}.")

add_student: Appends a student to the school's student list.

remove_student: Removes a student from the list if enrolled; otherwise, it prompts a message.

add_teacher: Adds a teacher to the school's teacher list.

remove_teacher: Removes a teacher from the list if present; otherwise, it displays a message.

  • if __name__ == “__main__”:

Imagine we have a bunch of tools in a toolbox. Each tool has its own name, but there’s a special tool called the “main” tool. When we open the toolbox, we want to use this “main” tool first because it helps organize and use the other tools effectively.

In Python, __name__ is like a label on each tool. When you open your Python toolbox (run a Python script), Python checks these labels to see if any tool is labeled as "main." If a tool has the label "main," Python knows that this tool is important for starting things off—it's the one we want to use first.

So, when we see if __name__ == "__main__": in a Python script, it's like Python saying, "Hey, if this tool is the main one we're starting with, let's do these specific things with it." This part of the code helps Python know what to do first when we run our script directly. It's like a special instruction saying, "Start here!"

if __name__ == "__main__":
my_school = School("ABC School", "123 Main St")
my_school.add_student("Alice")
my_school.add_student("Bob")
my_school.add_teacher("Mr. Smith")

print(f"Students in {my_school.name}: {my_school.students}")
print(f"Teachers in {my_school.name}: {my_school.teachers}")

my_school.remove_student("Charlie")
my_school.remove_teacher("Ms. Johnson")
  • if __name__ == "__main__":: In Python, the __name__ variable holds the name of the current module. When a Python script is executed, its __name__ variable is set to "__main__" if it's the main program that's running.
  • my_school = School("ABC School", "123 Main St"): This line creates an instance of the School class named my_school, passing in the name ("ABC School") and location ("123 Main St") as arguments to the constructor (__init__ method).
  • my_school.add_student("Alice") and my_school.add_student("Bob"): These lines call the add_student method of the my_school instance to add students named "Alice" and "Bob" to the school.
  • my_school.add_teacher("Mr. Smith"): This line uses the add_teacher method to add a teacher named "Mr. Smith" to the school.
  • print(f"Students in {my_school.name}: {my_school.students}") and print(f"Teachers in {my_school.name}: {my_school.teachers}"): These lines print out the current list of students and teachers enrolled at my_school.
  • my_school.remove_student("Charlie") and my_school.remove_teacher("Ms. Johnson"): These lines attempt to remove a non-existent student ("Charlie") and teacher ("Ms. Johnson") from the respective lists. If the student or teacher is not found, it prints a message indicating that they are not enrolled or not a teacher at that school.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response