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 main navigation

Extending an existing class

Extending an existing class
0.5
This week, we’re going to extended an existing class. But it’s also perfectly normal to write your own classes and then extend them. We’re just going to test this class out first. So create a new Trinket. If you’re using a text editor, create a new file called character test . py. Copy the code from this link and save it as character . py. If you’re using a text editor, make sure this file is in the same folder as all of your previous code.
29
This is the character class, which we’ve written for you. You can see from looking at the code that to create a character object, we must provide two arguments to the constructor, the character’s name, and a description of the character. Go back to your test file. Inside this file, create a character object.
55.1
Call the method describe on the object you created to show the character’s description on the screen. Save and run your programme, and you should see the description of the character you just created.
Dave
This week we’re going to extend an existing class. This means we will create another class which uses the functionality of the existing class, but also adds to or overwrites some of it. It’s also perfectly normal to write your own classes and then extend them.
If you are using a text editor, copy the code from this script, and paste and save it as a new file named character.py in the folder containing all your earlier code. If you are using Trinket, start a new trinket and create a character.py file to store the code, but keep the main.py file as well.
The code is the Character class which we have written for you – if you have a look at it, you should recognize some familiar things.
You’ll see that to create a Character object, the constructor needs two parameters – the character’s name, and a description of the character:
def __init__(self, char_name, char_description):
self.name = char_name
self.description = char_description
self.conversation = None
If you are using a text editor, create a new Python file and save it as character_test.py in the folder with your other code. If you are using Trinket, put this code inside the main.py file in the new trinket. Inside this file, create a character object.
from character import Character
dave = Character("Dave", "A smelly zombie")
Call the method describe on the object you created to show the character’s description on the screen.
dave.describe()
Save and run your program. You should see the description of the character you just created:
Dave is here!
A smelly zombie

Challenge

  • Examine the Character class inside character.py to find the name of the method which sets the conversation attribute
  • Add code to main.py to call this method and give Dave a line of dialogue
  • Add code to main.py to call a different method which talks to Dave
So far, so good – we can use this code as the basis for our characters. However, not every character in the game will have the same characteristics. Some will be friends and some will be enemies, and they may behave differently.
This article is from the free online

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

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education