0

Could anyone help with my understanding, please? I don't understand what is happening with this line or why it works : course_running.add_student(self).

I thought this was an OOP concept but could anyone help make this clearer?

class Student:
    def __init__(self, name, student_number):
        self.name = name
        self.student_number = student_number
        self.classes = []

    def enrol(self, course_running):
        self.classes.append(course_running)
        course_running.add_student(self)

class CourseRunning:
    def __init__(self, course, year):
        self.course = course
        self.year = year
        self.students = []

    def add_student(self, student):
        self.students.append(student)
wencakisa
  • 5,850
  • 2
  • 15
  • 36

2 Answers2

1

course_running is an object of class CourseRunning and course_running.add_student(self) is calling a method of it's class named add_student which is appending the student to students list.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

Your enrol() function in the Student class is taking two parameters: self and course_running.

self is the instance of your current class (Student).

That's why in your add_student() function (which takes also two parameters: self (the current instance of the CourseRunning class) and student (which is simply an instance of a Student)).

That's why you can pass the self from enrol() as student in add_student().

wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • If I "pass self from enrol() as student in add_student()", doesn't it append the list with an object. This is where i'm getting confused. If i'm putting an object into a list how is that useful? – Joe Greaves Aug 17 '17 at 16:31