OOP – Private Attributes (underscore convention)

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

OOP – Private Attributes (underscore convention)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does a single underscore _ prefix mean?
A Convention: internal use, don't access directly
B The variable is deleted
2. Does Python enforce single-underscore privacy?
A Yes you can't access it
B No it's just a convention
3. What does double underscore __ do?
A Deletes the variable
B Name mangling makes it harder to access
4. Why use getters and setters?
A To control and validate access to attributes
B They're faster
5. def get_balance(self): what is this?
A A getter method that returns the balance
B A constructor

Match the Pairs

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

Left columnRight column
1obj.__balance AError: Use snake_case: get_balance
2def getBalance(self): BError: Double underscore triggers name mangling
3self._balance COk: Getter method to access private data
4def get_balance(self): DOutput: 1
510 // 3 EOutput: 3
610 % 3 FValid: Single underscore signals internal use

Definitions

_variable

Fill in the Blanks

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

1.
class Account: def ________(self, bal): self._balance = bal
2.
class Account: def get_balance(____): return self._balance
3.
class Account: def deposit(self, amount): if amount _ 0: self.________ += amount
4.
class Secret: def ________(self, val): self.__val = val
5.
acc = Account(100) print(acc.___________( ))

Spot the Error

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

Question 1
A acc = BankAccount(100)
B acc._balance = -500
C print(acc.get_balance())
Question 2
A class Account:
B def __init__(self, bal):
C self.__balance = bal
D
E acc = Account(100)
F print(acc.__balance)
Question 3
A class Account:
B def deposit(self, amount):
C self._balance += amount
Question 4
A class Person:
B def __init__(self, age):
C self._age = age
D def set_age(self, age):
E self._age = age
Question 5
A class Account:
B def __init__(self, bal):
C self._balance = bal
D def get_balance():
E return self._balance

Answer Key

OOP – Private Attributes (underscore convention)
Section A: Quick Questions
Q1
A — Convention: internal use, don't access directly
Q2
B — No it's just a convention
Q3
B — Name mangling makes it harder to access
Q4
A — To control and validate access to attributes
Q5
A — A getter method that returns the balance
Section B: Match the Pairs
Q1
obj.__balance → Error: Double underscore triggers name mangling
Q2
def getBalance(self): → Error: Use snake_case: get_balance
Q3
self._balance → Valid: Single underscore signals internal use
Q4
def get_balance(self): → Ok: Getter method to access private data
Q5
10 // 3 → Output: 3
Q6
10 % 3 → Output: 1
Section C: Definitions
Q1
_variable — Convention for internal use - don't access directly from outside the class.~~__variable
Section D: Fill in the Blanks
Q1
class Account: def __init__(self, bal): self._balance = bal
Q2
class Account: def get_balance(self): return self._balance
Q3
class Account: def deposit(self, amount): if amount > 0: self._balance += amount
Q4
class Secret: def __init__(self, val): self.__val = val
Q5
acc = Account(100) print(acc.get_balance( ))
Section E: Spot the Error
Q1
B — Directly setting _balance bypasses validation. Use deposit() method.
Q2
F — __balance is name-mangled. Access through a getter method.
Q3
C — No validation should reject negative deposits
Q4
E — Setter has no validation should check age > 0
Q5
D — Missing self parameter in get_balance method