Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Writing a Module

In this lesson, you’ll learn the fundamentals of writing a module. There are three ways to define a module in Python:

  1. A module can be written in Python itself.
  2. A module can be written in C and loaded dynamically at run-time, like the re (regular expression) module.
  3. A built-in module is intrinsically contained in the interpreter, like the itertools module.

A module’s contents are accessed the same way in all three cases: with the import statement.

Here, the focus will mostly be on modules that are written in Python. The cool thing about modules written in Python is that they are pretty straightforward to build. All you need to do is create a file that contains legitimate Python code and then give the file a name with a .py extension. That’s it! You don’t need any special syntax.

For example, suppose you have created a file called mod.py containing the following:

s = "Computers are useless. They can only give you answers."
a = [100, 200, 300]

def printy(arg):
    print(f'arg = {arg}')

class Classy:
    pass

Several objects are defined in mod.py:

  • s: a string
  • a: a list
  • printy(): a function
  • Classy: a class

Assuming mod.py is in an appropriate location, which you’ll learn more about shortly, these objects can be accessed by importing the module as follows:

>>>
>>> import mod
>>> mod.a
[100, 200, 300]
>>> mod.s
'Computers are useless. They can only give you answers.'
>>> mod.printy('What is up!')
arg = What is Up!
>>> x = mod.Classy()
>>> x
<mod.Classy object at 0x03C181F0>

This courses uses the VS Code editor in the coding examples. If you’d like to learn more about this editor setup, please consult our VS Code Setup Guide.

Here are a few links for details on using bpython, the REPL (Read–Eval–Print Loop) tool used in most of these videos:

00:00 In this video, you’ll get an introduction to modules by writing a short module of your own. There’s three different styles of modules in Python. A module can be written in Python itself, but modules can also be written in C and then loaded dynamically at run-time.

00:16 And then there’s also built-in modules that are intrinsically contained inside the interpreter itself, and you may have already imported a few of them in the past, such as math, or importing random or datetime. For this video, I’ll have you try the first option—writing a simple module in Python itself.

00:34 Let me have you try this out in an editor. For the code in this course, I’ve created a directory named ModulesAndPackages. If you want to code right along with me, go ahead and create that directory.

00:46 And inside that, you need to make a new file and name it mod.py.

00:53 mod.py is a standard Python file. Inside of it, you’re going to create a string object

01:09 and a small list,

01:14 and define a function—let’s just call it printy(). printy() takes an argument called arg, and printy() prints using an f-string.

01:25 There is an arg (argument), and that f'arg = {arg}'. The arg that you’re entering in, you’re calling the variable in the f-string.

01:32 If you haven’t worked with f-strings, I’ll include a link to some articles that take you a little bit further into f-strings. Then, make a class. I’ll have you call it capital Classy.

01:42 There’s your Classy class, and right now that class doesn’t really do anything. It just passes. Okay, so a very simple file, mod.py. Saving. Down here in my terminal,

01:52 I’m going to create a REPL session using a third-party REPL that’s called bpython. I’ll include links in the tutorial text below this video also about that.

02:01 So in this case, instead of typing python or python3 and getting the standard REPL, instead I’ll type bpython, and you’ll see a few things that are a little bit different as I use it. Here you are in Python in an interpreter.

02:15 What you can do then is type in import. Since you are in the same directory where you created the file, you can say import mod. Okay, what did that do? By importing mod, you’ve imported all the objects that were part of that code. You could say, “Okay, well, what is mod.a?” Well, mod.a is that list that you created earlier. Or mod.s? Well, that’s that string. If you wanted to use printy(), you could say, “Okay, mod.printy() is now available also.” The argument is 'What is Up!' and printy() prints out that the arg = What is Up! And you could make a new object named x using mod.Classy. What is x?

02:55 Well, x is a Classy object from the module mod. Cool! So you have access to all of these different objects by importing that module, allowing for a lot of reusability of code.

03:09 Now that you’ve imported something, in the next video you’ll focus on where the interpreter searches for those files that you’re importing.

Become a Member to join the conversation.