Professional Documents
Culture Documents
A2 Worksheet - Using Getter Methods - OOP - KS4
A2 Worksheet - Using Getter Methods - OOP - KS4
Below is the code that you should have in your Pet class:
class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
self.description = description
A. Add the following method below your existing code that will return the name
of your pet:
def get_name(self):
return self.name
B. Using the method above as a guide, add two more methods to your code to
get the species of your pet and the description.
print(my_cat.get_name())
Task 3 . Challenge
Instead of three separate print statements in your mypet.py file, we will use a different
approach to get information about our object.
Open up your pets.py file and add an additional method called describe() which will
print out the object’s attributes.
Below is an example of your mypet.py file calling the new describe method and the
related output:
Output
Fluffy is a Cat. Black and white long haired.
>>>
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.