These questions follow Attributes and Methods and Encapsulation. A method is where a rule lives; encapsulation is the practice of making that the only place it lives, and of being honest about how much Python will help you.

Python does not enforce privacy

Several answers below depend on this being said plainly: a single leading underscore is a convention, and a double underscore is name mangling. Neither one stops anybody. If an answer here claims otherwise, it is wrong.

Reading and fixing

  1. In card.use(), what is self? Explain what Python does with the object between the dot and the parenthesis.
  2. Predict the output, all three lines.
    class Shift:
        def __init__(self, volunteer, hours):
            self.volunteer = volunteer
            self.hours = hours
     
        def extend(self, extra):
            hours = self.hours + extra
     
        def describe(self):
            print(f"{self.volunteer}: {self.hours} h")
     
    s = Shift("Nadia", 2)
    s.extend(1.5)
    s.describe()
    print(s.describe())
  3. Rewrite describe so that it can be used in a report, an email, and a test — and say why that is better than printing.
  4. This line appears in a teammate’s file: card._visits_left = 99. The code runs. Explain what the underscore means, what Python does about it, and what you would write in the review.

Writing

  1. Write a PunchCard class for the drop-in gym: an owner, a private-by-convention _visits_left, a top_up(visits) that refuses zero or negative amounts, a use() that returns False on an empty card, and a visits_left() reader. Show it running out and being topped up.
  2. Here is a class with a rule that can be bypassed. Encapsulate it, and show the refused change:
    class Booking:
        def __init__(self, room, people):
            self.room = room
            self.people = people   # must be 1 to 30
  3. Extend somebody else’s code. You did not write this class and may not change what is already there. Add is_long(), which is true for shifts of three hours or more, and split(), which returns two shifts of half the length.
    class Shift:
        """One volunteer shift at the homework club."""
     
        def __init__(self, volunteer, hours):
            self.volunteer = volunteer
            self.hours = hours
     
        def __str__(self):
            return f"{self.volunteer}: {self.hours} h"
  4. What does a double leading underscore actually do? Demonstrate it, including the way around it.
  5. Judgement. For a Member class holding name, _visits_left, _emergency_contact, and joined_on, say which attributes deserve the underscore and why. Then say which one you would argue for not storing at all.

Answers

Curriculum connection

A2.2

use modular design concepts that support reusable code (e.g., encapsulation, inheritance, method overloading, method overriding, polymorphism);

Link to original

A4.3

create fully documented program code according to industry standards (e.g., doc comments, docstrings, block comments, line comments);

Link to original

C1.2

demonstrate the ability to apply data encapsulation in program design (e.g., classes, records, structures);

Link to original

C1.4

apply the principle of reusability in program design (e.g., in modules, subprograms, classes, methods, and inheritance).

Link to original