Modules Flashcards

(23 cards)

1
Q

Why should you use modules in Python?

A

Because definitions (functions, variables) are lost when the interpreter exits. Modules let you preserve and reuse code across scripts.

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

What is a module?

A

A .py file containing Python code (functions, variables, classes). It can be imported into scripts or the interactive interpreter.

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

What is the purpose of \_\_name\_\_ in a module?

A

It’s a global variable set to the module name, or '\_\_main\_\_' when the file is executed as a script.

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

How do you import a module and use a function from it?

A

Use import module_name, then call the function with module_name.function().

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

How do you import a specific function from a module?

A

Use from module import function. Example: from fibo import fib.

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

What does from module import * do and why is it discouraged?

A

It imports all non-underscore-prefixed names. It’s discouraged because it can overwrite existing names and pollutes the namespace.

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

How can you assign an alias to an imported module?

A

Use import module as alias. Example: import fibo as fib.

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

How do you rename a function on import?

A

Use from module import function as new_name. Example: from fibo import fib as fibonacci.

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

How do you write a module to work both as a script and as an importable module?

A

Wrap script logic in if \_\_name\_\_ == '\_\_main\_\_':. Example:

```python
if __name__ == ‘__main__’:
import sys
fib(int(sys.argv[1]))
~~~

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

In what order does Python search for modules?

A
  1. Built-in modules, 2. Script directory, 3. Directories in PYTHONPATH, 4. site-packages (default).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is sys.path and how can it be used?

A

sys.path is a list of directories Python searches to find modules. It can be modified at runtime using list operations.

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

Where are compiled Python files stored?

A

In the \_\_pycache\_\_ directory, with names like module.cpython-XY.pyc.

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

How does Python decide to recompile a module?

A

Python recompiles if the source file is newer than the .pyc file or if the .pyc is missing.

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

What do the -O and -OO flags do?

A

-O removes assert statements. -OO removes assert and docstrings, creating optimized .pyc files.

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

What does dir() do?

A

Lists names defined in a module or the current local scope.

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

How do you list built-in names in Python?

A

Import the builtins module and run dir(builtins).

17
Q

What is a package in Python?

A

A directory with an \_\_init\_\_.py file, allowing a hierarchical structure using dotted module names.

18
Q

How do you import submodules from a package?

A

Use syntax like import package.subpackage.module or from package.subpackage import module.

19
Q

What is \_\_all\_\_ used for in a package’s \_\_init\_\_.py?

A

Defines which modules to import when using from package import *. Example:

```python
__all__ = [‘echo’, ‘surround’, ‘reverse’]
~~~

20
Q

What happens if \_\_all\_\_ is not defined?

A

Only names defined in \_\_init\_\_.py or already imported submodules will be available.

21
Q

What are relative imports and how are they written?

A

They use dots to refer to sibling/parent modules. Examples:

```python
from . import echo
from .. import formats
~~~

22
Q

Can the main script use relative imports?

A

No. Relative imports only work within packages, not in the main script.

23
Q

What is \_\_path\_\_ used for in packages?

A

It’s a list of directories Python uses to search for submodules in a package. It can be modified dynamically.