Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 1 secondNow let's create some getters and setters. These are methods that get and set the values of the objects attributes. Here is a method to set the description of the room. For every method within a class, the first parameter specified must be self, the object itself, followed by any data you wish to pass in, just as we did with the constructor.

Skip to 0 minutes and 22 secondsHere is a method which returns the description of the room. Add these methods inside the room.py file after the constructor method.

Getters and setters

We want to be able to interact with our room object, so let’s create some getters and setters. These are methods that get and set the values of the object’s attributes.

For every method within a class, the first parameter specified must be self – the object itself – followed by any data you wish to pass in, just as we did with the constructor. Add these methods inside the room.py file, after the constructor method.

add-code screenshot

Here is a method to set the description of the room:

def set_description(self, room_description):
    self.description = room_description

Here is a method to get the description of the room:

def get_description(self):
    return self.description 

Challenge

challengeicon

Add a getter method called get_name and a setter method called set_name to your class to allow someone to get and set the attribute name in the same way.

You can see a solution for this challenge at the end of the next step.

Using getters and setters

Let’s use the set_description method we created earlier to give our kitchen object a description. Go back to your main.py file and add this line of code below the line where you instantiate the object:

kitchen.set_description("A dank and dirty room buzzing with flies.")

Did you notice that the method we wrote had two arguments (self and room_description), but in the code above we only had to provide the room description? The self argument is always given first when we write a method, but when we use a method, we do not have to give this argument to it. This is because it is automatically passed to the method when you use Python. It may work slightly differently for other programming languages.

We can also use the get_description method we created earlier. Add this line at the bottom of your main.py file:

kitchen.get_description()

Save and run your program. You will probably be expecting to see the description of your kitchen room on the screen. However, when you run the program, you will not see anything.

This is because our get_description() method returns the description rather than printing it. This is useful if we want to do something else with the description, such as concatenate it with some other text, but if we want to display the description, we need to use the print command.

Instead of amending the get_description() method, we will use a different approach. Go back to room.py and add an additional method called describe() which will print out the object’s description when called.

def describe(self): 
    print( self.description )

In main.py, change the code to call the describe() method instead of get_description(), then run it to see the description appear.

'replace-code' screenshot

A note about our methods

In Python you don’t strictly need getters and setters because you can access the attributes of the object directly (e.g. you could just print( kitchen.description )). However, in other languages, such as Java, attributes can be protected or private. Therefore, we have chosen to use explicit getters and setters in this course to allow you to transpose the principles to other languages. Moreover, including getters and setters in your class is good programming etiquette, because they are useful tools for ensuring that people are using your code in the way you expect, and for minimising the risk of unexpected errors.

Python also has a more advanced way of specifying getters and setters for attributes using the @property decorator. While this is probably considered more ‘pythonic’, we have chosen not to use this method in this course, to keep things simple and avoid teaching a syntax that is specific to Python. However, you may want to investigate this method for yourself if you intend to do a lot of OO programming in Python, and this article is a good place to start.

Share this video:

This video is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation

Contact FutureLearn for Support