U1LA5.1: Your Custom Function Library
How can I create a library to store my custom functions?
Teacher Notes and Overview
In this lesson, students will share what they created in the last mini-project by sharing the code itself for others to use. This is a great skill to learn that collaboration does not need to look like pair programming, and it's also a terrific time for students to get creative!
This will also introduce the idea of having multiple files in one program to keep their code organized. Encourage students to follow this practice as much as possible to stay neat and orderly; their main program file should just be the run of the program, and as much as possible we should hide our (working) functions away so they don't get accidentally messed with and also do not clog up our screen.
Objectives
Students will be able to:
Import libraries from other Python documents
Store functions in a separate Python document
Share custom functions
Utilize the functions of their peers
Suggested Duration
1 Period (~45 minutes)
This could easily become a two-period lesson if you would like students to spend more time creating with the functions they were given. Please plan accordingly!
NYS Standards
9-12.CT.4 Implement a program using a combination of student-defined and third-party functions to organize the computation.
9-12.CT.5 Modify a function or procedure in a program to perform its computation in a different way over the same inputs, while preserving the result of the overall program.
9-12.DL.1 Type proficiently on a keyboard.
9-12.DL.2 Communicate and work collaboratively with others using digital tools to support individual learning and contribute to the learning of others.
Vocabulary
Library: a document that contains a bunch of functions
Planning Notes and Materials
Resources
Custom Functions Library Template
Custom Functions Libraries (Youtube Video)
Assessments
Formative:
Custom Function Library Project
Summative:
Abstract Album Art (Upcoming Unit Project)
Do Now/Warm Up (~2 - 3 minutes)
Ask students to open their custom emoji and also a tab with a fresh project in Trinket (or whatever editor you choose to use). Depending on the time allotted, students can make any final tweaks to their emoji functions. (Stress that this is the function, not the design they made with it that they will be adjusting!)
Creating Documentation (~5 - 10 minutes)
Since the start of the year, you've been using functions written by other people and documentation that explains how they work. It is always a best practice to leave documentation in your code - and especially within your function - with the assumption that someone else might be using your code one day. (And, honestly, that someone else might just be Future You.)
An easy way to do this is to leave a special kind of comment called a docstring at the top of your function. In some code editors, this will auto-generate helper text that explains the function when you start using it! We want to describe not only what our function does, but what the inputs do, as well. And we should add our name for good measure! So if we use the very first happyFace()
function we wrote, the documentation might look like this:
NB: Eventually, they should also describe the return of a function, but that is not necessary yet as their functions do not have return values and it would be a lot to explain!
Ask students to take a moment to document their function by writing a multi-line comment directly above it. It should include one line to explain what the function does, and then explanations for any and all parameters.
Sharing Functions && Documentation (~7 - 12 minutes)
Share students on the Custom Functions Library Template.
Please note that you should just make one per class - in especially large classes, you may have students do this in batches.
Ask students to copy/paste their documentation and function into the document. Remind students not to style the font - it is currently set to a monotype font which generally copy/pastes well into code editors! (Other fonts can do strange things with formatting.) At this point, ask students to change their emoji name just in this document to include their first name before it, such as courtneyHappyFace()
.
Eventually, students will be creating their own function library, and functions must be uniquely named to work as expected!
From there, have students go to the blank project they opened as part of the do now. They will be creating a new Python file to house their functions and linking it to the project so it's usable. This section is best done as a code-along!
In Trinket, click the + button which will say 'Create text file' when you hover over it. Once you do so, you'll create a new file - name it functions.py. Note that it MUST have the .py otherwise the computer will not recognize this as a Python file!
Now, we need to connect the two files, but first, let's load in some functions. Have students start by copy/pasting their own emoji function into the functions.py document. (We will have time to add more functions later, but this is enough to get us started and make sure our linked files communicate.) Because our functions rely on the Processing.py library, we will also need to add a line at the top:
And now it's time to learn what that means and why it's important! Let's head back to our main.py file.
Python has a lot of libraries - some external, and some internal like our funcitons.py sheet - that we can use in our main programs. There are a few ways to do this. The first is to use import
, which will allow us to import everything in functions.py.
Using import
would look something like this:
Now, a few things for us to notice: first, we imported the library at the top of our page. This will always be our practice so we can use the functions for the entire program. When we called our function, we had to write functions.happyFace()
- this is so the program knows where to find the function definition. As we can imagine, this can get annoying! One way to get around it is to save an instance of a function to an alias, which we don't need to cover now since we have a few other options.
Rather than using import
, we can use from
. from
has two distinct uses. In the first, we use it only import select functions:
Now we don't need to use functions. because happyFace()
has been locally scoped! But what if we have a lot of functions to use? Well, we can look at how processing has been imported as a reference. If we do something like this:
...we will have access to all of the functions in functions.py! This is great if you are using most of a library. If you are not, stick with the plain from
as it will save a little bit of processing room in your program.
But for now, we need to fill up our library with more functions.
Using Your Custom Function Library (10 - 15 minutes)
Ask students to identify several functions they want to use and copy/paste each into their functions.js file. They should use these to create an interesting composition in their new project. Remember to save to use this as a reference later!
Wrap-Up (~5 minutes)
There are no planned extensions for this lesson. Students should keep creating with the given functions, but they can also modify functions to make their own version!
Last updated