Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 7 · Class Diagram

Class Diagram

On this pageUniversity Course Model

A class diagram shows types, attributes, operations and relationships. Its rectangles have compartments for the class name, data and operations.

+ means public, - private and # protected in UML. Our Python _enrollments and _grade use the internal-use underscore convention; Python does not enforce those UML access levels.

{readOnly} marks a public value that callers read without assigning to it directly.

1 means exactly one; 0..* means zero or more. Plain lines show associations. We use them here because students and courses have independent lifetimes.

University Course Model

Student+student_id : str+name : str#_enrollments : list+enroll(course) : Enrollment+course_codes() : listCourse+course_code : str+title : str+credits : int#_enrollments : list+student_names() : listEnrollment+student : Student+course : Course#_grade : str or None+grade : str or None {readOnly}+set_grade(value)student0..*1course0..*1

Read one box from top to bottom: its class name, attributes, then operations. An attribute such as name : str describes stored text; an operation such as enroll(course) : Enrollment accepts a course and returns an Enrollment. Parentheses identify an operation, while a colon introduces a stated type.

Read a relationship from both directions. The 0..* near Enrollment allows one Student to have zero or many enrollments; the 1 near Student says each Enrollment refers to exactly one Student. The Course relationship works the same way.

Student’s enroll(course) creates an Enrollment and records the same link on both sides. Enrollment holds the grade for that student in that course. Its public grade property is read-only; set_grade controls updates.

Check the model: why would a single grade stored on Course lose information? Trace a second student joining the same course, then inspect the object snapshot.