Learn

We’re keeping things organized by storing our classes in separate files, so let’s do a brief review on how to use code from another file.

Python gives us the ability to import code from another file. Here’s how we can use our Vertex class from within graph.py.

# from <file_name> import <class> # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside of ./vertex.py file class Vertex # code for the class... # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside of ./graph.py file from vertex import Vertex my_vertex = Vertex("Import Accomplished!")

Instructions

1.

Use the file explorer to tab between vertex.py, graph.py, and script.py. When you’re ready to continue tab over to script.py.

Inside of script.py, import Graph and Vertex from their respective files.

2.

Make an instance of Graph and assign it to the variable railway.

3.

Make an instance of Vertex with the string "Ballahoo" and assign it to the variable station.

4.

Call .add_vertex() on railway and pass station as the argument.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?