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

Wes House
Wes House
6,944 Points

Why "hand = hands.Hand(size=5, die_class=dice.D6)" rather than "hand = Hand(size=5, die_class=dice.D6)" in the console?

I don't understand why hands.Hand() part. Why do we need to call the name of the file with an attribute of Hand(size=5, die_class=dice.D6)?

1 Answer

Steven Parker
Steven Parker
229,982 Points

You didn't provide a link to a course page, but I'd guess that "hands" was imported as a module and the class "Hand" wasn't specifically imported:

import hands            # with this you must call hands.Hand()
#----------------------------- OR ----------------------------
from hands import Hand  # with this you can simply call Hand()
Wes House
Wes House
6,944 Points

Sorry, it's from this lesson: https://teamtreehouse.com/library/giving-a-hand

And the issue I'm referring to happens at 2:15.

Steven Parker
Steven Parker
229,982 Points

I guessed it! At 2:08 he says "And let's import dice, and let's import hands.". He does that with this code:

import dice, hands

So those imports give you access to the files, but not directly to the classes within them. You still need to name the module to get to the class. And that explains "hands.Hand()".

If he had done the other kind of import, then "Hand()" could be used.