Modules and Variable Scope

Modules

One of the strengths of Python is that there are many built-in add-ons - or modules - which contain existing functions, classes, and variables which allow you to do complex tasks in only a few lines of code. In addition, there are many other third-party modules (e.g. Numpy, Scipy, Matplotlib) that can be installed, and you can also develop your own modules that include functionalities you commonly use.

The built-in modules are referred to as the Standard Library, and you can find a full list of the available functionality in the Python Documentation.

To use modules in your Python session or script or functions, you need to import them. The following example shows how to import the built-in math module, which contains a number of useful mathematical functions:

In [ ]:
import math

You can then access functions and other objects in the module with math.<function>, for example:

In [ ]:
math.sin(2.3)
In [ ]:
math.factorial(10)
In [ ]:
math.pi

Because these modules exist, it is often the case that there are already modules and functions that can do what you want to do. This will make your code much more readable and efficient.

For example, the numpy module, which we will talk about tomorrow, contains useful functions for finding e.g. the mean, median, and standard deviation of a sequence of numbers:

In [ ]:
import numpy as np
In [ ]:
li = [1,2,7,3,1,3]
np.mean(li)
In [ ]:
np.median(li)
In [ ]:
np.std(li)
In [ ]:
li=[1,2,3]
print(np.std(li,ddof=1), np.sqrt((1+0+1)/2))   # Bessel factor
In [ ]:
np.std?

Notice that in the above case, we used:

import numpy as np

instead of:

import numpy

which shows that we can rename the module such that it is more convenient to access the module's variables, functions etc.

Finally, it's also possible to simply import the functions needed directly:

In [ ]:
from math import sin, cos
print(sin(3.4))
print(cos(3.4))

You may find examples on the internet that use e.g.

from module import *

but this is not recommended, because it will make it difficult to debug programs, since common debugging tools that rely on just looking at the programs will not know all the functions that are being imported.

Where to find modules and functions

How do you know which modules exist in the first place? The Python documentation contains a list of modules in the Standard Library, but you can also simply search the web. Once you have a module that you think should contain the right kind of function, you can either look at the documentation for that module, or you can use the tab-completion in IPython:

In [2]: math.<TAB>
math.acos       math.degrees    math.fsum       math.pi
math.acosh      math.e          math.gamma      math.pow
math.asin       math.erf        math.hypot      math.radians
math.asinh      math.erfc       math.isinf      math.sin
math.atan       math.exp        math.isnan      math.sinh
math.atan2      math.expm1      math.ldexp      math.sqrt
math.atanh      math.fabs       math.lgamma     math.tan
math.ceil       math.factorial  math.log        math.tanh
math.copysign   math.floor      math.log10      math.trunc
math.cos        math.fmod       math.log1p      
math.cosh       math.frexp      math.modf    

Exercise 1

  • Does the math.cos funtion take radians or degrees?

  • Search for functions that can convert between radians and degrees.

  • Use these to find the cosine of 60 degrees, and the sine of $\pi/6$ radians.

In [ ]:
# enter your solutions here

Variable scope

Local scope

In the following example, the variables defined in the function are not available outside the function:

In [ ]:
def do_something():
    a = 1
print(a)

The variable a is defined in the local scope of the function.

Global scope

Consider the following example:

In [ ]:
a = 1

def show_var():
    print(a, b)

b = 2
show_var()

In this case, the function knows about the variables defined outside the function. The variables are in the global scope. This is very useful because it means that modules don't have to be imported inside functions, you can import them at the top level:

In [ ]:
import numpy as np

def normalize(x):
    return x / np.mean(x)
In [ ]:
a=[5,3,5,7,8,3]
normalize(a)

This works because modules are objects in the same sense as any other variable. In practice, this does not mean that you should ever use:

In [ ]:
a = 1
def show_var(x):
    print(x)
    
show_var(a)

because it makes the code harder to read. The exception to this are modules and variables that remain constant during the execution of the program. One exception to this is if you need to define constants (such as pi, or physical constants). See the PEP8 section below for more details.

Local scope has precedence over global scope

Consider the following example:

In [ ]:
a = 1

def show_var():
    a = 2
    print(a)

show_var()

What happened? Variables defined anywhere inside a function are part of the local scope of the function. Any variable in the local scope takes precedence over any other variable, even before it is actually used:

In [ ]:
def show_var():
    print(a)
    a = 2
    
show_var()

In this case, a is defined inside the function and so it doesn't matter if a is used anywhere else in the Python code. The above function will therefore not work because a is used before it is defined.

Exercise 2

What will the following code print? (think about it, don't run it!):

def double(x):
    x = x * 2
    print(x)

x = 1
double(x)
print(x)

and what about this code?:

def append_3(x):
    x.append(3)
    print(x)

x = [1,2]
append_3(x)
print(x)
In [ ]:
# Ok, you can try them out now!
def double(x):
    x = x * 2
    print(x)

x = 1
double(x)
print(x)


def append_3(x):
    x.append(3)
    print(x)

x = [1,2]
append_3(x)
print(x)

PEP8 style

[=Python Enhancement Proposal 8]

We just touched on the idea of constants being used in functions - but Python does not really have constants, so how do we recognize them? We now need to speak about coding style.

There is a set of style guidelines referred to as PEP8, which you can find here. These guidelines are not compulsory, but you should follow them as much as possible, especially when you have to work with other people or need other people to read your code.

You don't need to read the guidelines now, but I will first give a couple of examples, then I will show you a tool that can help you follow the guidelines. The following example does not follow the style guidelines:

In [ ]:
pi = 3.1415926
def CalculateValues(x):
    return(x*pi)

Constants should be made uppercase, and function names should be lower case separated by underscores (the so called camel-case used above is reserved for classes).

This is the correct way to write the code:

In [ ]:
PI = 3.1415926

def calculate_values(x):
    return(x * PI)

Other examples include that indentation should always be 4 spaces, etc. In practice, you can check your code with this script although this does not work in the notebook. Download the pycodestyle.py script from Github and upload it to your working folder. Then also create a Python script called my_script.py locally with the following content:

#!/usr/bin/env python

# This is a very long comment that will be much longer than the desired linelength of 79 chars

def do_something():
   print ('We are doing something...')

do_something()

and upload it to the same folder on the Jupyter Server. Go to the overview of all your files on the Jupyter Server and select "Terminal" from the "New" menu item. A new Browser tab with an open terminal wll appear. In this terminal, run:

python pycodestyle.py my_script.py

where my_script.py is the script you just created and that you want to check. In the above example, you will see:

my_script.py:3:80: E501 line too long (94 > 79 characters)
my_script.py:5:1: E302 expected 2 blank lines, found 1
my_script.py:6:4: E111 indentation is not a multiple of four
my_script.py:6:9: E211 whitespace before '('
my_script.py:8:1: E305 expected 2 blank lines after class or function definition, found 1
my_script.py:8:15: W292 no newline at end of file

The errors include the line number and you can adjust your code according to PEP 8.

One additional point about PEP 8:

Make sure your code is never longer than 79 characters (with 72 characters a desirable limit).

Why?

  1. 80 characters is about what the average human can understand “at a glance”, without having to visually follow lines and such. That's why books are printed the way they are. And why xterms and such should be 80 columns wide.
  2. When using version control, lines of that size still make useful diffs (i.e. changes from one version to another). Have longer lines, and you have lots of spurious material people have to scan to find what's actually changed.

This is true not only for python source code. Make it a habit for TeX, markdown etc.