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) Hack-n-Slash Game Planning

Why import all the individual sub-classes instead of just the main module?

In this Object Oriented | Game Planning video, Kenneth creates game.py and goes on to import all the necessary modules:

from character import Character
from monster import Dragon
from monster import Goblin
from monster import Troll

My question then is, why not import the entirety of the class Monster from monster? Even if we don't need the Monster class, don't the sub-classes inherit from Monster anyway?

Thanks guys. Looking forward to working this out.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I am not sure what you mean by "the entirety of the class Monster".

Even if Monster was imported using from monster import Monster, the other classes would still need to be explicitly imported. The base class Monster knows nothing of the other classes. The classes Dragon, Troll, and Goblin all extend the base class Monster and may override parts of the Monster definition. So importing Monster serves no purpose unless you intend to define other new monster classes that also inherit from Monster, or if you wish to create an instance of Monster.

You could use import monster, and then all of the class references need the dot notation:

import monster
from character import Character

# reference to monster classes using dot notation:
Puff = monster.Dragon()
Griphook = monster.Goblin()
Gothmog = monster.Troll()
Herman = monster.Monster()

chris freeman ,

Thanks for clarifying this for me.

I realized that my mistake was thinking that the sub-classes Dragon, Goblin, Troll were indented functions within the Monster class. I know now that is incorrect.

The sub-classes, as you mentioned, are classes of their own and NOT functions. Importing just Monster would not allow me to access the attributes of the sub-classes.

Glad I got this down. Thanks again for the help!

Cheers