在班级上添加清单

时间:2018-09-27 09:10:49

标签: python

class Course:
    ''' 
    A class representing a course offering that includes the following
    information about the course: subject, course number, section,
    enrolment cap, lecture days, lecture start time, lecture duration 
    in minutes, and unique student numbers of the students enroled.
    If lectures occur on multiple days, they will all start at the same time.
    '''

    def __init__(self, subject, number, section, cap, days, start_time, dur):
        '''
        returns a new Course object with no students enroled, given
           the subject, number, section, cap, days, start_time, and dur
        __init__: Str Nat Nat Nat Str Time Nat -> Course
        requires: number is a 3-digit number, section > 0, cap > 0,
           days is string containing substrings representing the days
           of the week 'M', 'T', 'W', 'Th', 'F', where if the course is
           offered on more than one day, the days appear in order and
           are separated by a single dash. For example, 'M-T-Th'
           indicates the course is offered on Monday, Tuesday, and Th.
        '''
        self.subject = subject
        self.number = number
        self.section = section
        self.cap = cap
        self.days = days
        self.start_time = start_time
        self.dur = dur

    def add_student(self, student_id):
        '''
        adds a student to the course enrolment if there is room in the course
           if the number of students enroled already matches the 
           enrolment cap, then print the message "Course full"
           if the student is already enroled in the course, the there is no 
           change to the Course object, and the message "Previously enroled"
           is printed.
        add_student: Course Nat -> None
        Effects: Mutates self. May print feedback message.
        '''
        pass            

对于方法add_student,如果不在 init 方法中(不能将其添加到INIT方法中),我将如何实现列表?该列表需要与该对象连接,因此以后我可以从该列表中删除学生。

3 个答案:

答案 0 :(得分:2)

您可以改为使用__new__方法添加它:

class Course:
    def __new__(cls, *args, **kwargs):
        course = super(Course, cls).__new__(cls)
        course.students = []
        return course

答案 1 :(得分:1)

天真的方法:尝试成员是否存在,捕获属性错误,如果不存在则创建它。

def add_student(self, student_id):
    try:
        self.__list
    except AttributeError:
        self.__list = []
    self.__list.append(student_id)

最好使用“ getter”来确保在通过任何方法访问列表时都创建了该列表:

def get_list(self):
    try:
        self.__list
    except AttributeError:
        self.__list = []
   return self.__list

然后add_student变为:

def add_student(self, student_id):
    self.get_list().append(student_id)

当然,如果您没有一些奇怪的限制,最好将其添加到__init__中。

答案 2 :(得分:0)

您可以使用property getter初始化列表,以便在首次访问该列表时对其进行初始化:

class Course:
    @property
    def students(self):
        try:
            return self._students
        except AttributeError:
            self._students = []
            return self._students

    @students.setter
    def students(self, value):
        self._students = value