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

Instantiating your own object

Instantiating your own object
0.9
Now 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.
43.9
When 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.
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.
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