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

jsdevtom
jsdevtom
16,963 Points

Is there benefit in importing the individual submodules instead of the whole module in Python?

I'm aware that there is a similar question here. It doesn't go in to the benefits though. I have also tried a (admittedly brief) google search.

Is there benefit in doing:

from monster import Dragon
from monster import Goblin
from monster import Troll

Puff = Dragon()

as opposed to:

import monster

Puff = monster.Dragon()

or vise versa? Thanks.

2 Answers

Hi Tom!

I guess the answer to this question depends on how much you are actually going to use from what you are importing. If you had a general purpose monster module but knew you were only going to be using Dragons in this particular instance then 'from monster import Dragon' would be the better way to go about it. This would be better because you are only actually importing what you need and you aren't clogging up your application with imported code that isn't actually being used anywhere.

However, if you do need the contents of the whole module then, yeah, it is a lot simpler to just do the following: 'import monster'

I hope that I managed to clear that up for you. If you need any more help on this topic then don't hesitate to let me know! I hope you're having a great day and have fun programming :)

There is a whole lot of benefits, when it comes to launching the program. Now you won't see it as the game is a light weight application, but when working with bigger more complex frameworks and apps the loading time is crucial. Imagine if you would need to use a screwdriver, would you take the whole toolbox with you or just the screwdriver you need?

Python takes the whole 'load everything you might need it' away. For example Ruby does that.