Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Subclass

Sean Maden
Sean Maden
3,504 Points

Not sure what is being asked (create class named Dragon that extends to Monster class...import Monster from monster)

In this question, are we coding a subclass in a class library file, or writing code from the shell that imports the class library? Don't we define a subclass by editing the class file, and not from the shell prompt?

1 Answer

Hi Sean,

The general syntax for creating a class that extends another is:

class subclassName(baseClass):
  pass

We need to create a class called Dragon that extends the class Monster which is defined for us in a file called monster. We'll need to import this in order to use it - and, to use Monster without having to prefix it with monster, like monster.Monster we'll import this as follows:

from monster import Monster

We are importing the Monster class from within the monster file.

Please let me know if this helps or not.

Cheers