9. Modules and Packages Flashcards

1
Q

What extension does a module have>

A

.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is a module accessed?

A

Use the import command. Normally at the top of the file.

If we want to import the math module, we simply import the name of the module:

# import the library
import math
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What two functions are helpful when exploring modules?

A

dir()

help()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The dir() function does what?

A

We can look for which functions are implemented in each module by using the dir function:

print(dir(math))

[‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the help() function do?

A

we can read about a method using the help function, inside the Python interpreter:

help(math.ceil)

Help on built-in function ceil in module math:

ceil(…)
ceil(x)

Return the ceiling of x as an Integral.
This is the smallest integer >= x.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create a module?

A

Writing Python modules is very simple. To create a module of your own, simply create a new .py file with the module name, and then import it using the Python file name (without the .py extension) using the import command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a package?

A

Packages are name-spaces which contain multiple packages and modules themselves. They are simply directories, but with a twist.

Each package in Python is a directory which MUST contain a special file called init.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.

If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the init.py file inside the foo directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does importing a module look like?

A

To use the module bar, we can import it in two ways:

# Just an example, this won't work
import foo.bar
# OR could do it this way
from foo import bar
How well did you know this?
1
Not at all
2
3
4
5
Perfectly