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

[Solved] NameError: name 'Monster' is not defined

Below code give above error. Not sure why?

import monster

class Dragon(Monster): size = 10

my_monster.py
import monster

class Dragon(Monster):
    size = 10

2 Answers

You need to use

from monster import Monster

otherwise you would need to use

class Dragon(monster.Monster):

for it to work, but the problem is asking for the above example where you use the from monster import Monster statement.

I just tested it in the code challenge and they will actually both work. So you can choose whichever you prefer.

Just to clarify, here would be either code you could use:

import monster

class Dragon(monster.Monster):
    size = 10
from monster import Monster

class Dragon(Monster):
    size = 10

This is because the first import statement is importing the entire monster file so you need to then specify that you want the Monster class from the monster.py file. If you use the from monster import Monster, then you are just importing the Monster class and can refer to it just as Monster.

Thanks for the quick response John Lindsey This solves the issue :)