Skip to content

Instantly share code, notes, and snippets.

@Gastove
Created May 31, 2016 18:15
Show Gist options
  • Save Gastove/72613c0d38d2b6433335f61ddc4f6d71 to your computer and use it in GitHub Desktop.
Save Gastove/72613c0d38d2b6433335f61ddc4f6d71 to your computer and use it in GitHub Desktop.

Modules

In python, a "module" is a set of related code. We work with modules every time we use import:

import datetime #  <- `datetime` is totally a module
            

Modules are often built up in to "packages" and then installed via pip, or else are included (like datetime) in python's robust "standard library" (all the things that come with python by default). But: we can also make our own modules! Let's say we have this structure:

tree
            
            .
            |-- a_module
            |   |-- __init__.py
            |   `-- poot.py
            `-- app.py

            1 directory, 3 files
          

In poot.py, we have:

def go_poot():
              print('poot')
            

The __init__.py file means that a_module is, well, a module. It's a marker to the python interpreter. It can contain a lot of python code, but in fact, most often is a completely empty file. It just has to exist in a directory to tell python, "you can import this." So now in apd.py, we can do this:

import a_module

              a_module.poot()
            

And get poots. Yay? Yay.

Author: Ross Donaldson

Created: 2016-05-31 Tue 11:13

Validate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment