← Python functions and events

Modules

Keep related functions together in a file you can import, like a toolbox.

Every Python program you will ever use imports other people's modules. Now you will build your own.

You have written several functions. A module is a file that holds related functions, so any program can import them.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

modules.py
from turtle import *
from shapes import square, triangle

square(80)
triangle(80)

This program never defines square or triangle. What does it draw? Where do you think they come from?

RunRun it and compare the result with your prediction.

Run

Run it. The line from shapes import square, triangle brings two ready-made functions into your program.

modules.py
from turtle import *
from shapes import square, triangle

square(80)
triangle(80)
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

One bug, two copies

Two separate programs both need wall, so each has its own copy. Nothing here crashes; the problem is the copying. Pasting from My tools works the same way: each paste is a copy, and editing the tool does not change copies already pasted. This is the problem a module solves.

The walls should close into squares. Find every place the fix has to go.

You fix one copy. What about the other?

The walls do not close: the turn inside wall is wrong. Click every line you would have to change.

program_a.py
program_b.py

You fix the bug in program_a.py. What happens to program_b.py?

InvestigateTrace how each line of code connects to what you see.

Where does it come from?

Open Reference, find Module: shapes and click Show how it works. Compare it with the functions you have written.

modules.py
from turtle import *
from shapes import square, triangle

square(80)
triangle(80)

Where does square come from?

shapes.py
from turtle import *


def square(size):
    for i in range(4):
        forward(size)
        right(90)
main.py
from turtle import *
from shapes import square


def square(size):
    circle(size / 2)


square(80)
Try it yourselfYour edits are saved in this browser.
Where is square defined?

main.py imports square, then defines its own square. What does square(80) draw?

ModifyChange the code on purpose and check the result.

Modify

These three functions are all in one file, and the wall does not close. Click + module, move them into it, and import house back into main.py. Then fix the turn in wall, once, in the module. Work it out from the shape, as you did with variables: 360 / 4, not a guessed number.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Click + module, give it a name, then cut the three functions and paste them there.

Another hint

Your program needs a line at the top: from yourname import house

Another hint

The walls turn 80 degrees. Work the turn out from the shape instead: right(360 / 4).

InvestigateTrace how each line of code connects to what you see.

Mix it up

Work this out on paper first.

# shapes.py
def square(size):
    for i in range(4):
        forward(size)
        right(90)

# main program
from shapes import *
square(30)
square(50)

How far does the turtle draw in total, counting only the lines (not the turns)?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Create a module of your own with at least three related functions. Import it and use it to draw a small scene.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Pick a theme for your module, such as trees, stars or buildings.

Another hint

Keep drawing calls in main.py. The module should only contain functions.

ModifyChange the code on purpose and check the result.

Extend a street

A street is a few buildings repeated, and each building comes from a module. Run it. Add a roof colour, a taller house, or a gap in the street. You are changing the plan without rewriting any of the shapes.

Try it yourselfYour edits are saved in this browser.
For teachers

Students import functions from a module (a separate file of related functions), see why copied code costs more to fix, then build a module of their own. About 55 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

A module is a Python file of functions that other programs can use. from shapes import square means "find the file shapes.py and bring in its square function". In the playground, + module makes a new file tab. Before class, do the Modify step yourself: move the three house functions into a module and import house.

Running the lesson

  1. Predict: the program draws a square and a triangle but never defines them. Ask where they might come from.
  2. Investigate (one bug, two copies): both programs have the same wrong turn. Fixing it means editing twice, and missing one leaves a bug. That cost is the reason modules exist.
  3. Point out that My tools works the same way as copying: each paste is a separate copy.
  4. Investigate (where does it come from?): open the shapes module from the Reference panel. Ask: what happens if main.py defines its own square after importing one?
  5. Modify: students move three functions into a module, import house, and fix the turn once.
  6. Make: a theme helps (trees, stars, buildings). Keep the drawing calls in main.py; the module only holds functions.

The hard part (on purpose)

Seeing why a module beats copying, and that a def with the same name replaces an import. Ask how many places they'd have to fix if the code were copied.

When students get stuck

Stretch and support

Stretch: Write a module with a docstring for each function (a first line in triple quotes). The docstring becomes the hint on the function's card in the Reference panel.

Support: Do the Modify step together on the projector, then let students make a two-function module before attempting three.

Where it leads

Next unit makes programs react to clicks and keys. Every real Python program imports modules others wrote (random, math, and libraries like Gradio). Class ideas: each student builds a module for a theme and a classmate imports it; a class town built from everyone's modules.

What each step is for

Now you can
Next lesson7. EventsMake a program wait, then react when something happens.