Hi! Today we're going to talk about modules. A module, in its essence, is no more than a script, a file containing Python code. When you're working on a big project, you might want to split your code in smaller modules to keep it manageable; additionally, using modules makes possible to reuse the same code in several projects.
The most prominent case of reuse is obviously the Python Standard Library. Python comes with a set of standard modules that add a lot of useful features to those built into the language. You only need to import them into your own program and use them. How?
In Python, you do that with the import keyword, followed by the name of the module:
import someLibrary
From now on, someLibrary works as a namespace to access the functions offered the the module. That is: in order to call some function defined in someLibrary, you're going to use:
someLibrary.someFunction()
Coding the function to evaluate the factorial is a good exercise. But using the library function which is included in the math module is certainly faster:
import math
print math.factorial(10)
Using the namespace to access the functions from a module reminds us where that function comes from, especially when reading code written by other people or by ourselves a long time ago. But sometimes it's overkill: do you really need to be reminded that factorial is in the math module? For that, Python provides a more concise syntax, using the from keyword:
from math import pi
radius = 4
print 2 * pi * radius
Notice that now the module is imported with from, while import specifies which attributes (in this case a constant, pi) you want to extract from math and bring into your code.
This is consistent with the fact that you're not importing the math module! If you try to call math.factorial(10) in the second program you get an error ("math is not defined") because we imported just pi, not the whole module.
However, we can specify multiple attributes in our from ... import directive:
from math import pi, factorial
radius = 4
print 2 * pi * radius
print factorial(10)
or even importing all the attributes at once using:
from math import *
radius = 4
print 2 * pi * radius
print factorial(10)
Notice that in the last version of the code there's really no way to infer where pi and factorial come from by just reading the code. You just happen to know it or you don't (or you have good IDE...)
Therefore, the from keyword (and the wildcard version especially) should be reserved for standard libraries or when playing in the Python shell, since the namespacing makes the code clearer.
It's also the best choice when you're importing two attributes with the same name, as rare as this situation might be.
- The code in this review: Gist - Fiddle
- Python Koans
- Modules in the Python Tutorial
A classic problem: frequency count.
Given a list of objects (numbers and strings are OK, but feel free to pick your favourite type as long as "A equals B" is defined) create a dictionary with the objects in the list as keys and the number of their occurrences (how many times the same object is stored in the list) as the value. Example
input = [6, 45, 23, 6, 34, 2, 98, 45, 6]
output = {6: 3, 45: 2, 23: 1, 34: 1, 2: 1, 98: 1}
Now, you can implement your own logic or import the [Counter] (http://docs.python.org/2/library/collections.html#collections.Counter) class from the collections module and use it. Your choice.