Contact FutureLearn for Support
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 that we have written some code, let's test it out by creating an object of the Room class we just created. Create a new Python file and save it in the same folder as the file containing your room code, but call this file main.py. If you are using Trinket, main.py already exists. In this file, add the following line of code. This line of code looks for a file in the same folder called room.py and then looks inside that file for a class called Room, with an uppercase R. This is the class we just wrote. Now we will instantiate a Room object like this. We gave the Room object the name Kitchen so that we can refer to it later.

Skip to 0 minutes and 44 secondsWhen you run your program, nothing will happen. A Room object is being created, but we can't see it or interact with it, as we haven't written any methods yet.

Instantiating your own object

Now that we have written some code, let’s test it out by creating an object of the Room class we have written.

If you are using Trinket, you will already have a main.py file. If you are using a text editor, create a new Python file called main.py and save it in the same folder as the file room.py.

In the main.py file, add the following line of code:

from room import Room

This command looks for a file in the same folder called room.py, and looks inside that file for a class called Room (upper-case R) – this is the class we just wrote. It then makes that class available for use inside the main.py file. (If you did not save your room code as room.py, this will not work – in this case, you will need to rename the file.)

We can now instantiate (create) a Room object like this:

kitchen = Room("Kitchen")

We are giving the object the name ‘kitchen’ so that we can refer to it later.

kicthen

If you save and run your program, nothing will happen. Our Room object is being created, but we can’t see it or interact with it, because we didn’t write any methods when we created the Room class.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation