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

Creating an instance of a class

Why do we need to run the following command before creating an instance of a class

from characters import <class name>

For example , in order to create an instance of the class "Student"

from characters import Student Ken=Student()

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Very good question! Each module being executed has its own namespace where all object references are stored. Since Student was defined in another module it is not present in the current module namespace and is therefore undefined. An import statement is used to bring the object in.

The import statement uses the Python module search path to find the object definition and add it to the local namespace.

Once the import is done, the Student class definition can be referenced as if it had been defined locally.

Post back if you need more help. Good luck!!!