Warm tip: This article is reproduced from serverfault.com, please click

python-是否可以创建嵌套的类属性?

(python - Is it possible to create nested class attributes?)

发布于 2020-11-28 00:15:27

我是Python的新手,最近学习了有关类的知识。我一直在尝试他们,并提出了一个学生/课程评分系统。这是到目前为止的代码:

class course:
    TOTAL_COURSES = 0

    def __init__(self, name, room, max_students):
        self.name = name
        self.room = room
        self.max_students = max_students
        self.student_list = []
        course.TOTAL_COURSES += 1
 
    def addStudent(self, student):
        # Checks if the class is below max capacity
        if len(self.student_list) < self.max_students:
            self.student_list.append(student)
            # Adds the course to the student's course list
            student.course_list.append(self)
            return True
        return False

因此,这将创建一个课程班,我可以添加学生并设置他们的房间和其他内容。我还有另一堂课,旨在存储有关学生的信息:

class student:
    TOTAL_STUDENTS = 0

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender
        # Courses in course_list are stored as objects
        self.course_list = []
        student.TOTAL_STUDENTS += 1

很简单;我只是停留在如何创建实际的评分系统上。如果我要这样做:

s1 = student("Tom", 17, "Male")
c1 = course("English", "E123", 25)

然后可以使用“嵌套属性”吗?所以我将那个学生的课程成绩分配给一个像这样的值:

s1.c1.grade = "A+"

但是,这不起作用,并抛出(预期的)AttributeError。所以我必须使用以前创建的course_list吗?

s1.course_list[0].grade = "A+"

即使那样,我也不确定如何为该课程对象分配成绩。

Questioner
General_Mudkip
Viewed
0
user6386471 2020-11-28 10:20:58

这是通过为学生分配“课程位置”而不是课程本身来解决上述一些问题的解决方案。你可能会想到,可用的课程槽位数量受到限制,具体取决于课程的最大大小。该代码可以进一步开发,但是我认为这对入门非常有用:

class Student:

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender
        self.courses = {}
        
    def addCourse(self, course):
        if course.status=='Enrolled':
            self.courses[course.name] = course
        else:
            self.courses[course.name] = course.status

            
class Course:

    def __init__(self, name, room, max_students):
        self.name = name
        self.room = room
        self.max_students = max_students
        self.student_list = []
        
        self.course_slots_filled = 0
        self.course_slots_available = max_students
        
    def __str__(self):
        return 'Course_object_{}'.format(self.name)
 
    def check_slots_available(self):
        if self.course_slots_filled < self.max_students:
            return True
        else:
            return False
    
    class CourseSlot:
        def __init__(self, name, student_name, status):
            self.name = name
            self.student_name = student_name
            self.status = status
            self.grade = 'No grade assigned'
        
        def __repr__(self):
            return 'CourseSlot_object_{}'.format(self.name)
        
        def set_grade(self, grade):
            self.grade = grade
            
    
    def assign_course_slot(self, student_name):
        if self.check_slots_available():
            self.course_slots_filled+=1
            self.course_slots_available-=1
            status = 'Enrolled'
            self.student_list.append(student_name)
        else:
            print('Sorry, {} class is full! {} not enrolled.'.format(self.name, student_name))
            status = 'Not enrolled'
            
        return self.CourseSlot(self.name, student_name, status)

用法示例

实例化课程:

physics = Course('Physics','101',5)
chemistry = Course('Chemistry','102',1)

实例化学生1并分配课程时段:

s1 = Student("Tom", 17, "Male")
s1.addCourse(physics.assign_course_slot(s1.name))
s1.addCourse(chemistry.assign_course_slot(s1.name))
s1.courses['Physics'].set_grade('A+')

[v for v in s1.courses.values()]

# >>> [CourseSlot_object_Physics, CourseSlot_object_Chemistry]

实例化学生2并分配课程时段

s2 = Student("Susan", 18, "Female")
s2.addCourse(physics.assign_course_slot(s2.name))
s2.addCourse(chemistry.assign_course_slot(s2.name))

#>>> Sorry, Chemistry class is full! Susan not enrolled.

获取课程信息:

print('Physics course slots filled: ',physics.course_slots_filled)
print('Physics course slots available: ',physics.course_slots_available)
print('Chemistry course slots filled: ',chemistry.course_slots_filled)
print('Chemistry course slots available: ',chemistry.course_slots_available)

#>>> Physics course slots filled:  2
#    Physics course slots available:  3
#    Chemistry course slots filled:  1
#    Chemistry course slots available:  0

print('Physics student list: ',physics.student_list)
print('Chemistry student list: ',chemistry.student_list)

# >>> Physics student list:  ['Tom', 'Susan']
#     Chemistry student list:  ['Tom']

for s in [s1,s2]:
    for c in s.courses.values():
        try:
            print('{} {} grade: {}'.format(s.name, c.name, c.grade))
        except AttributeError:
            pass

# >>> Tom Physics grade: A+
#     Tom Chemistry grade: No grade assigned
#     Susan Physics grade: No grade assigned

我猜作弊是,该课程student_list仅获取学生的姓名,而不是Student对象的名称,如果你将其传递给唯一的ID,然后遍历Student对象的列表以匹配ID,则可能会起作用。无论如何都要考虑一下。