You are on page 1of 2

KS4 – Object-oriented programming Learner Activity sheet

Lesson 2 – Classes and objects

Activity 3 worksheet: Using setter


methods
To set information in our objects, we need to write some
methods that allow us to change it. These are called setters.
They are essentially functions that can be called to set
information about our objects.

Use the information on this sheet to complete activity 3 in


your pairs.

Task 1 . Creating a set method

Previously we created get methods to retrieve information about our objects. What if we
want to set/change information? For this we use set methods that allow us to change
information within our objects.

An example set method has been created in the Pet object below:

class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
self.description = description

def set_name(self, name):


self.name = name

Modify your pets.py class file to include the set_name() method above and check it
works by calling it in your my_pets.py file.

Task 2 . Check the getters


Page 1 Last updated: 25-01-21
KS4 – Object-oriented programming Learner Activity sheet
Lesson 2 – Classes and objects

Add two additional methods to your Pet object that allow you to set the species and
description.

Resources are updated regularly — the latest version is available at: ncce.io/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see ncce.io/ogl.

Page 2 Last updated: 25-01-21

You might also like