Getter methods and @property

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

Getter methods and @property

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which method is a getter for the private attribute self.__name?
A def get_name(self): self.__name = name
B def get_name(self): return self.__name
2. Why do we write a getter method for a private attribute?
A So outside code can read the value without touching the attribute directly
B So outside code can delete the class
3. Which line correctly calls a getter method named get_name() on an object called pet?
A print(pet.get_name)
B print(pet.get_name())
4. Which line goes directly above a getter to turn it into a property?
A @property
B @getter
5. A class has a getter marked with @property called name. How do you read it?
A print(pet.name)
B print(pet.name())

Match the Pairs

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

Left columnRight column
1def get_name(self): return self.__name AUsing brackets on a @property. A property is read like an attribute, so it should be pet.name.
2@property Bstudent.name = 'Sam' # AttributeError
3Outside code reads a private attribute by calling a getter method CA decorator that lets a getter method be read like an attribute, with no brackets
4A property with no setter is read-only, so assigning to it causes an error Dprint(account.get_balance())
5print(pet.__name) EAccessing the private attribute directly from outside the class. Call a getter such as pet.get_name() instead.
6print(pet.name()) FA getter method that returns the private attribute __name so outside code can read it

Definitions

@property

Fill in the Blanks

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

1.
class Student: def ________(self): return self.__name
2.
_________ def name(self): return self.__name
3.
acct = Account(50) print(acct._______) # no brackets needed
4.
@property def balance(self): return self._________
5.
try: acct.balance = 500 except ______________: print("Read-only")

Spot the Error

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

Question 1
A class Student:
B def __init__(self, name):
C self.__name = name
D def get_name():
E return self.__name
Question 2
A class Pet
B def __init__(self, name):
C self.__name = name
D @property
E def name(self):
F return self.__name
Question 3
A class Book:
B def __init__(self, title)
C self.__title = title
D def get_title(self):
E return self.__title
Question 4
A class Car:
B def __init__(self, make):
C self.make = make
D @property
E def make(self):
F return self.__make
Question 5
A class Car:
B def __init__(self, make):
C self.__make = make
D @property()
E def make(self):
F return self.__make

Answer Key

Getter methods and @property
Section A: Quick Questions
Q1
B — def get_name(self): return self.__name
Q2
A — So outside code can read the value without touching the attribute directly
Q3
B — print(pet.get_name())
Q4
A — @property
Q5
A — print(pet.name)
Section B: Match the Pairs
Q1
def get_name(self): return self.__name → A getter method that returns the private attribute __name so outside code can read it
Q2
@property → A decorator that lets a getter method be read like an attribute, with no brackets
Q3
Outside code reads a private attribute by calling a getter method → print(account.get_balance())
Q4
A property with no setter is read-only, so assigning to it causes an error → student.name = 'Sam' # AttributeError
Q5
print(pet.__name) → Accessing the private attribute directly from outside the class. Call a getter such as pet.get_name() instead.
Q6
print(pet.name()) → Using brackets on a @property. A property is read like an attribute, so it should be pet.name.
Section C: Definitions
Q1
@property — A decorator above a getter method. It lets outside code read a private attribute like a normal attribute, such as obj.name, with no brackets. With no setter, the attribute is read-only.
Section D: Fill in the Blanks
Q1
class Student: def get_name(self): return self.__name
Q2
@property def name(self): return self.__name
Q3
acct = Account(50) print(acct.balance) # no brackets needed
Q4
@property def balance(self): return self.__balance
Q5
try: acct.balance = 500 except AttributeError: print("Read-only")
Section E: Spot the Error
Q1
D — A getter method inside a class needs self as its first parameter so it can reach self.__name. It should be def get_name(self):.
Q2
A — A class header must end with a colon. It should be class Pet:.
Q3
B — A method definition must end with a colon. It should be def __init__(self, title):.
Q4
C — The private attribute must be stored as self.__make. Assigning to self.make clashes with the read-only @property (it raises an AttributeError) and leaves self.__make unset. It should be self.__make = make.
Q5
D — @property is a decorator and is written without brackets. Adding () causes an error. It should be @property.