Week 1 --- Python: Libraries

Sep 7, 2012 • Sarah Supp

 

What is a library?

Beginning programmers often learn that to keep code simple, readable, and non-repetitive, statements should be organized into useful chunks that can be reused, called functions. In a similar sense, libraries, also known as modules, do the same thing for functions. They are an important component of programming because they allow you to access and organize code into functional chunks that can be used again or shared with others. Storing related functions, statements, variables and constants in a module can be convenient but it also reduces unnecessary duplication and makes code easier to read, which in turn, reduces your likelihood of making an error.

Some modules are already built into Python (e.g., re, sys, and os), but others can be downloaded from third parties or developed directly yourself. In fact, every .py file can be used as a library by other programs. Once a module is installed on or saved to your computer, you can import it into a program to use the functions and variables stored within it.

How can I use a library?

To use a module, you can use a simple import statement, which should come at the beginning of your program. For an example, let’s look at a standard Python library, math. To access the functions within this module start with:

import math

When a module is imported for the first time in your programming session, Python executes all the statements within that module. This means that if the module contains only definitions for functions and variables, that it won’t seem like Python is doing much, but if the module contains print statements, these will display when the module is executed.

Python stores the imported module within an object, which is given the same variable name as the module. This means that to use the functions included within the module math, such as sqrt, we must call it using dot notation, which includes the module name:

math.sqrt()

Using dot notation may seem like a lot of extra typing, but it is important because it allows each module to have it’s own namespace. In other words, if two modules use the same name for a function or variable, Python is able to identify them separately and continue to use the functions correctly rather than accidentally masking one with the other. If this were not the case, then whichever module was imported last would contain the working version of the variable name. This could lead to some unexpected results because your program might not be doing what you think it’s doing!

If, however, you want to get around using dot notation, you can import specific functions directly from a module. For example, if you knew you didn’t have any variable name conflicts with sqrt, and importing the entire suite of functions from math was unnecessary, you could use:

from math import sqrt

Which now allows you to use sqrt without the dot notation:

sqrt()

Another option to avoid using dot notation is to import all the functions directly from a module, using the wildcard:

from math import * #use with caution

Making your own libraries

Importing a code that you have written as a library works the same as in the examples above, provided that you have saved your code as a .py file. For example, if you will be using a function or set of functions in multiple programs, you can save them in a separate file as myfunctions.py. Be sure to include in your module whatever import command are necessary for it to work. In another program, you can import it using:

import myfunctions

Note that you don’t need to include the .py extension.

Where can I learn more about libraries?

A few websites and books that may be helpful are listed below:

Standard Python Libraries

Practical Programming

Practical Computing For Biologists

Notes

It took me ~6.5 hrs to do this assignment, including the readings. I used CmapTools to make the concept map, which looks much sharper than my first attempt at it using powerpoint (Thanks for the suggestion, Justin Kitzes!)