In this lab, we will learn how to organize our functions into modules. Modules are files of reusable function. You’ve already encountered modules before!
💻 Go int your
lab_modules folder in your
💻 Update the packages
Don’t forget to change “YOURGITHUBUSERNAME” to your actual Github username.
git submodule update --init --recursive
 What is a module?
In the previous lab, we used functions to break up a big program into smaller programs. Modules do the same thing, but on a larger scale!
So let’s say you wrote a function in one file. What if you want to re-use a function you wrote in a different file? Copy it over? It feels like there should be a better way.
Today we are going to learn how to import code from one module into another. But first, let’s get some vocabulary straight:
- All python code lives in
filesthat end in
.py. They’re just plain text files that you can edit using Atom (or Microsoft Word if you really want. Don’t though. You’ll regret it.)
- Python thinks of each file as a
module. Each module is its own little bubble world.
- Python thinks of each directory containing
.pyfiles as a
package. A package is a bundle of modules. So your
unit_00directory is also a package.
[How to import a module]
When you want to use something from another module, you need to
We have actually already been doing this. Every one of our programs so far has started with
from turtle import *. But what is this
turtle? It’s a module! And it lives in a file called
turtle.py that some people wrote for you.
There are actually three different ways to get
First, is the way you are used to doing it. This way imports all of the functions in
turtle.py. It’s quick and easy, but it’s kind of sloppy and it’s not always a very good idea.
from turtle import * forward(100)
Or, you can import just the
forward function from
from turtle import forward forward(100)
Or, you can import the
Turtle objects. We will learn more about this in Unit 02: Games.
You can import
import turtle turtle.forward(100)
Imagine this scenario:
from bodily_functions import eat from refrigerator import * from trash_can import * eat(chicken_sandwich)
You can see that the
eat function came from
bodily_functions, but where did that
chicken_sandwich come from? The
refrigerator? Or the
trash_can? By importing just what we need from other modules, we can make it clear where everything came from, and we can make sure we don’t import stuff we don’t want. (What else did we import from the
trash_can? Do we want that in our program?)
Let’s start by going into
This folder has 3 files:
👀Take a look at
tree_parts.py. Note how
tree_parts.py uses functions from
tree.py is empty. It’s up to you to write function to draw a full tree. Your
tree_full() function should be able to draw a tree of any size.
Be sure to consider:
- what modules will you need to import?
- how can you combine exisiting functions to draw a full tree?
When you run
tree.py it look like:
 Drawing Package
Now we are going to use a package full of fancy drawing functions to turbocharge your turtle.
Packages always include documentation to communicate to users how to use the included software.
This lab will require you to read documentation provided in the
👀 Open the documentation for examples of all the functions available in the
drawingpackage. It has the following modules:
cd out of the
Here you should have:
fancy_drawing_example.py- an example usage of the drawing package
fancy_drawing.py- an empty file for you to create your own drawing
💻 Start by running:
- Why does the drawing just appear?
- What modules are used from the drawing package?
- How do the colors repeat?
💻 Experiment with editing the code. Try make your own version of this pattern! Here is just one of many potential patterns:
👀 Explore more color options HERE
fancy_drawing.py, it is up to you to create a drawing of your choosing using the modules in the drawing package. It must include uses of at least 3 different modules from the drawing package.
Be prepared to share your drawing!
Once you’ve successfully completed the sequence be sure to fill out this Google form.
💻 Push your work to Github:
- git status
- git add fancy_drawing.py
- git status
- git commit -m “describe your drawing and your process here”
be sure to customize this message, do not copy and paste this line
- git push