← VCE Software Development U4O1

Getter methods and @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.

Understanding Getter methods and @property in VCE Software Development U4O1

What it does: The getter is called with brackets, and it returns the private balance for printing. With only @property and no setter, trying to change the value fails with an AttributeError. A getter gives controlled, read access to a private attribute. The class stays in charge of its own data, which supports encapsulation. A normal getter is a method, so you need brackets to call it. Without them you only get the method itself, not the value. Python uses the built-in @property decorator. There is no @getter decorator in Python. @property sits above the getter so the student can write obj.name instead of obj.get_name().

Common mistakes: 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):. A class header must end with a colon. It should be class Pet:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 3–79) and word search (Modules 3–114).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which method is a getter for the private attribute self.__name?
def get_name(self): return self.__namedef get_name(self): self.__name = name
Matches
print(pet.__name)
↓
Accessing the private attribute directly from outside the class. Call a getter such as pet.get_name() instead.
Applying
Meanings
Term
@property
Meaning
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.
Practice
class Student:
Testing
Gaps
def (self):
Check Answer
Spots
3 self.__name = name
4 def get_name():

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Void Whale' badge — Definition Drifter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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 ✓
A getter only returns the value of the private attribute. Assigning to it would make the method a setter, not a getter.
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
A getter gives controlled, read access to a private attribute. The class stays in charge of its own data, which supports encapsulation.
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()) ✓
A normal getter is a method, so you need brackets to call it. Without them you only get the method itself, not the value.
4. Which line goes directly above a getter to turn it into a property?
A. @property ✓
B. @getter
Python uses the built-in @property decorator. There is no @getter decorator in Python.
5. A class has a getter marked with @property called name. How do you read it?
A. print(pet.name) ✓
B. print(pet.name())
A property is read like a normal attribute, with no brackets. Adding () tries to call the returned value, which causes a TypeError.

Match the Pairs

def get_name(self): return self.__name → A getter method that returns the private attribute __name so outside code can read it
@property → A decorator that lets a getter method be read like an attribute, with no brackets
Outside code reads a private attribute by calling a getter method → print(account.get_balance())
A property with no setter is read-only, so assigning to it causes an error → student.name = 'Sam' # AttributeError
print(pet.__name) → Accessing the private attribute directly from outside the class. Call a getter such as pet.get_name() instead.
print(pet.name()) → Using brackets on a @property. A property is read like an attribute, so it should be pet.name.

Key Term

@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.

Fill in the Blanks

1. class Student: def get_name(self): return self.__name
Hint: Type the name of the getter method that returns the private name attribute.
2. @property def name(self): return self.__name
Hint: Type the decorator that turns this method into a property that is read like an attribute.
3. acct = Account(50) print(acct.balance) # no brackets needed
Hint: Type the property name used to read the value, with no brackets.
4. @property def balance(self): return self.__balance
Hint: Type the private attribute that the getter returns.
5. try: acct.balance = 500 except AttributeError: print("Read-only")
Hint: Type the error type raised when you assign to a property that has no setter.

Spot the Error

Question 1
A class Student:
B def __init__(self, name):
C self.__name = name
D def get_name():
E return self.__name
Line 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):.
Question 2
A class Pet
B def __init__(self, name):
C self.__name = name
D @property
E def name(self):
F return self.__name
Line A — A class header must end with a colon. It should be class Pet:.
Question 3
A class Book:
B def __init__(self, title)
C self.__title = title
D def get_title(self):
E return self.__title
Line B — A method definition must end with a colon. It should be def __init__(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
Line 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.
Question 5
A class Car:
B def __init__(self, make):
C self.__make = make
D @property()
E def make(self):
F return self.__make
Line D — @property is a decorator and is written without brackets. Adding () causes an error. It should be @property.

Student with a class code? Join your class →