Protected attributes and methods

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Protected attributes and methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which attribute name marks a protected member in Python?
A self._balance
B self.balance_
2. Python stops outside code from reading a protected attribute such as obj._name.
A True
B False
3. A protected attribute _name is intended to be used...
A Inside the class and its subclasses
B Anywhere in the program
4. Which subclass method uses a protected attribute from its parent class correctly?
A def show(self): print(_name)
B def show(self): print(self._name)
5. Why do programmers use a single underscore on an attribute?
A To signal it is not part of the public interface
B To make Python block access to it

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1self._balance = 0 Aacc = Account() print(acc._balance)
2def _calculate_fee(self): BThe name has no leading underscore, so it is public. A protected attribute needs a single underscore: self._balance = 0.
3A subclass can use a protected attribute inherited from its parent class CDefines a protected method that is intended to be called only by the class itself or its subclasses
4Protected is a convention, not enforcement: code outside the class can still access the attribute DCode outside the class and its subclasses should not use a protected attribute directly. Use a public method or property such as acc.get_balance() instead.
5self.balance = 0 # meant to be protected ECreates a protected attribute: the single underscore signals it is meant for use only inside the class and its subclasses
6print(acc._balance) # in main program Fclass Savings(Account): def show(self): print(self._balance)

Definitions

_name

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Pet: _____ = "Rex" age = 3
2.
class Dog(Pet): def speak(self): return self._____ + " barks"
3.
class Account: def ______(self): return self.balance > 0
4.
acc = Account(50) print(acc.________) # Python allows this
5.
class Saver(Account): def show(self): print(self.______())

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Pet:
B def __init__(self, name):
C self._name = name
D class Dog:
E def show(self):
F print(self._name)
Question 2
A class Savings(Account)
B def add(self, amt):
C self._balance += amt
Question 3
A class Car:
B def __init__(self, make):
C self.make = make
D def info(self):
E return self._make
Question 4
A Class Staff:
B def __init__(self, pay):
C self._pay = pay
Question 5
A class Student:
B def _init_(self, name):
C self._name = name

Answer Key

Protected attributes and methods
Section A: Quick Questions
Q1
A — self._balance
Q2
B — False
Q3
A — Inside the class and its subclasses
Q4
B — def show(self): print(self._name)
Q5
A — To signal it is not part of the public interface
Section B: Match the Pairs
Q1
self._balance = 0 → Creates a protected attribute: the single underscore signals it is meant for use only inside the class and its subclasses
Q2
def _calculate_fee(self): → Defines a protected method that is intended to be called only by the class itself or its subclasses
Q3
A subclass can use a protected attribute inherited from its parent class → class Savings(Account): def show(self): print(self._balance)
Q4
Protected is a convention, not enforcement: code outside the class can still access the attribute → acc = Account() print(acc._balance)
Q5
self.balance = 0 # meant to be protected → The name has no leading underscore, so it is public. A protected attribute needs a single underscore: self._balance = 0.
Q6
print(acc._balance) # in main program → Code outside the class and its subclasses should not use a protected attribute directly. Use a public method or property such as acc.get_balance() instead.
Section C: Definitions
Q1
_name — A single underscore prefix marks a protected member: by convention it is for use inside the class and its subclasses only. Python does not enforce it, so outside code can still access it.
Section D: Fill in the Blanks
Q1
class Pet: _name = "Rex" age = 3
Q2
class Dog(Pet): def speak(self): return self._name + " barks"
Q3
class Account: def _check(self): return self.balance > 0
Q4
acc = Account(50) print(acc._balance) # Python allows this
Q5
class Saver(Account): def show(self): print(self._check())
Section E: Spot the Error
Q1
D — Dog must inherit from Pet to use the protected attribute _name. It should be class Dog(Pet): so subclass code can access self._name.
Q2
A — A class header must end with a colon. It should be class Savings(Account): so the subclass can use the protected _balance.
Q3
C — This creates a public attribute called make, not the protected _make. info() then fails because self._make does not exist. It should be self._make = make.
Q4
A — The class keyword must be lowercase in Python. It should be class Staff: before the protected attribute _pay is set.
Q5
B — The constructor needs double underscores on each side: __init__. A single underscore only marks a protected member, so Python would not call this when an object is created.