Summary
In this video I walk through building robust Python classes for a simple university enrollment model. I start by reviewing the standard class layout—attributes, methods, and an init constructor—then show how to customize object behavior with dunder methods. Implementing str gives readable print output for StudentId and MealPlan instances, while overriding eq lets equality be based on logical attributes instead of memory identity. I caution against using mutable objects (lists, dicts) as default argument values; instead I default to None and assign inside the method, using truthiness (e.g., self.enrolled_students = enrolled_students or []) to provide fallback values.
Next I construct a Course class from scratch, demonstrating optional attributes, default handling, and how composition works: a StudentId object holds a list of Course instances. The enrollInACourse method appends a Course to a student's courses list and also updates the Course's enrollment counter, illustrating side‑effects across related objects.
I then show how a central store of Course objects enables many students to share the same course reference. Enrolling a student both adds the course to the student's list and increments the course's enrollment count. I calculate total credits by summing each course's credit value (sum(course.credits for course in student.courses)) and obtain the number of courses with len(student.courses). The final exercise (Question 5) asks you to create a new Student, define new Course objects (e.g., Marine Bio, Art 150), enroll the student, and retrieve the total credits and course count. I also demonstrate using help(ClassName) to inspect available methods and attributes such as init, str, add_credit, enroll, upperclassman, and total_credit.
By the end of the session you should be able to define classes with proper dunder methods, avoid common pitfalls with mutable defaults, compose objects through lists of instances, and implement enrollment logic that tracks both student course lists and course enrollment totals.