You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Revision of Python - Modules and Packages
* Revision of Python - Modules and Packages
* Revision of Python - Modules and Packages
Co-authored-by: Aaron Redshaw <[email protected]>
Copy file name to clipboardExpand all lines: tutorials/learnpython.org/en/Modules and Packages.md
+40-56
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,25 @@ Tutorial
2
2
--------
3
3
4
4
In programming, a module is a piece of software that has a specific functionality.
5
-
For example, when building a ping pong game, one module would be responsible for the game logic, and
6
-
another module would be responsible for drawing the game on the screen. Each module is a different
7
-
file, which can be edited separately.
5
+
For example, when building a ping pong game, one module may be responsible for the game logic, and
6
+
another module draws the game on the screen. Each module consists of a different
7
+
file, which may be edited separately.
8
8
9
9
### Writing modules
10
10
11
-
Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file.
12
-
A Python module can have a set of functions, classes or variables defined and implemented.
13
-
In the example above, we will have two files, we will have:
11
+
Modules in Python are just Python files with a .py extension. The name of the module is the same as the file name.
12
+
A Python module can have a set of functions, classes, or variables defined and implemented.
13
+
The example above includes two files:
14
14
15
-
-mygame/
15
+
mygame/
16
16
17
17
- mygame/game.py
18
18
19
19
- mygame/draw.py
20
20
21
21
22
-
The Python script `game.py`will implement the game. It will use the function `draw_game` from the file `draw.py`,
23
-
or in other words, the`draw` module, that implements the logic for drawing the game on the screen.
22
+
The Python script `game.py`implements the game. It uses the function `draw_game` from the file `draw.py`,
23
+
or in other words, the`draw` module that implements the logic for drawing the game on the screen.
24
24
25
25
Modules are imported from other modules using the `import` command. In this example, the `game.py` script may
26
26
look something like this:
@@ -52,23 +52,20 @@ The `draw` module may look something like this:
52
52
...
53
53
54
54
In this example, the `game` module imports the `draw` module, which enables it to use functions implemented
55
-
in that module. The `main` function would use the local function `play_game` to run the game, and then
56
-
draw the result of the game using a function implemented in the `draw` module called `draw_game`. To use
57
-
the function `draw_game` from the `draw` module, we would need to specify in which module the function is
55
+
in that module. The `main` function uses the local function `play_game` to run the game, and then
56
+
draws the result of the game using a function implemented in the `draw` module called `draw_game`. To use
57
+
the function `draw_game` from the `draw` module, we need to specify in which module the function is
58
58
implemented, using the dot operator. To reference the `draw_game` function from the `game` module,
59
-
we would need to import the `draw` module and only then call `draw.draw_game()`.
59
+
we need to import the `draw` module and then call `draw.draw_game()`.
60
60
61
-
When the `import draw` directive will run, the Python interpreter will look for a file in the directory which
62
-
the script was executed from, by the name of the module with a `.py` suffix, so in our case it will try to
63
-
look for `draw.py`. If it will find one, it will import it. If not, he will continue to look for built-in modules.
61
+
When the `import draw` directive runs, the Python interpreter looks for a file in the directory in which the script was executed with the module name and a `.py` suffix. In this case it will look for `draw.py`. If it is found, it will be imported. If it's not found, it will continue looking for built-in modules.
64
62
65
-
You may have noticed that when importing a module, a `.pyc` file appears, which is a compiled Python file.
66
-
Python compiles files into Python bytecode so that it won't have to parse the files each time modules are loaded.
67
-
If a `.pyc` file exists, it gets loaded instead of the `.py` file, but this process is transparent to the user.
63
+
You may have noticed that when importing a module, a `.pyc` file is created. This is a compiled Python file.
64
+
Python compiles files into Python bytecode so that it won't have to parse the files each time modules are loaded. If a `.pyc` file exists, it gets loaded instead of the `.py` file. This process is transparent to the user.
68
65
69
66
### Importing module objects to the current namespace
70
67
71
-
We may also import the function `draw_game`directly into the main script's namespace, by using the `from` command.
68
+
A namespace is a system where every object is named and can be accessed in Python. We import the function `draw_game` into the main script's namespace by using the `from` command.
72
69
73
70
# game.py
74
71
# import the draw module
@@ -79,17 +76,14 @@ We may also import the function `draw_game` directly into the main script's name
79
76
draw_game(result)
80
77
81
78
82
-
You may have noticed that in this example, `draw_game` does not precede with the name of the module it is imported
83
-
from, because we've specified the module name in the `import` command.
79
+
You may have noticed that in this example, the name of the module does not precede `draw_game`, because we've specified the module name using the `import` command.
84
80
85
-
The advantages of using this notation is that it is easier to use the functions inside the current module because
86
-
you don't need to specify which module the function comes from. However, any namespace cannot have two objects
87
-
with the exact same name, so the `import` command may replace an existing object in the namespace.
81
+
The advantages of this notation is that you don't have to reference the module over and over. However, a namespace cannot have two objects with the same name, so the `import` command may replace an existing object in the namespace.
88
82
89
83
90
84
### Importing all objects from a module
91
85
92
-
We may also use the `import *` command to import all objects from a specific module, like this:
86
+
You can use the `import *` command to import all the objects in a module like this:
93
87
94
88
# game.py
95
89
# import the draw module
@@ -99,16 +93,16 @@ We may also use the `import *` command to import all objects from a specific mod
99
93
result = play_game()
100
94
draw_game(result)
101
95
102
-
This might be a bit risky as changes in the module might affect the module which imports it, but it is
103
-
shorter and also does not require you to specify which objects you wish to import from the module.
96
+
This might be a bit risky as changes in the module may affect the module which imports it, but it is
97
+
shorter, and doesn't require you to specify every object you want to import from the module.
104
98
105
99
106
100
### Custom import name
107
101
108
-
We may also load modules under any name we want. This is useful when we want to import a module conditionally
102
+
Modules may be loaded under any name you want. This is useful when importing a module conditionally
109
103
to use the same name in the rest of the code.
110
104
111
-
For example, if you have two `draw` modules with slighty different names - you may do the following:
105
+
For example, if you have two `draw` modules with slighty different names, you may do the following:
112
106
113
107
114
108
# game.py
@@ -128,11 +122,9 @@ For example, if you have two `draw` modules with slighty different names - you m
128
122
129
123
### Module initialization
130
124
131
-
The first time a module is loaded into a running Python script, it is initialized by executing the code in the
132
-
module once. If another module in your code imports the same module again, it will not be loaded twice but
133
-
once only - so local variables inside the module act as a "singleton" - they are initialized only once.
125
+
The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded again, so local variables inside the module act as a "singleton," meaning they are initialized only once.
134
126
135
-
This is useful to know, because this means that you can rely on this behavior for initializing objects.
127
+
You can then use this to initialize objects.
136
128
For example:
137
129
138
130
@@ -155,29 +147,28 @@ For example:
155
147
156
148
### Extending module load path
157
149
158
-
There are a couple of ways we could tell the Python interpreter where to look for modules, aside from the
159
-
default, which is the local directory and the built-in modules. You could either use the environment
160
-
variable `PYTHONPATH` to specify additional directories to look for modules in, like this:
150
+
There are a couple of ways to tell the Python interpreter where to look for modules, aside from the
151
+
default local directory and built-in modules. You can use the environment variable `PYTHONPATH` to specify additional directories to look for modules like this:
161
152
162
153
PYTHONPATH=/foo python game.py
163
154
164
-
This will execute `game.py`, and will enable the script to load modules from the `foo` directory as well
155
+
This executes `game.py`, and enables the script to load modules from the `foo` directory, as well
165
156
as the local directory.
166
157
167
-
Another method is the `sys.path.append` function. You may execute it *before* running an`import` command:
158
+
You may also use the `sys.path.append` function. Execute it *before* running the`import` command:
168
159
169
160
sys.path.append("/foo")
170
161
171
-
This will add the `foo` directory to the list of paths to look for modules in as well.
162
+
Now the `foo` directory has been added to the list of paths where modules are looked for.
163
+
172
164
173
165
### Exploring built-in modules
174
166
175
167
Check out the full list of built-in modules in the Python standard library [here](https://docs.python.org/3/library/).
176
168
177
169
Two very important functions come in handy when exploring modules in Python - the `dir` and `help` functions.
178
170
179
-
If we want to import the module `urllib`, which enables us to create read data from URLs, we
180
-
simply `import` the module:
171
+
To import the module `urllib`, which enables us to create read data from URLs, we `import` the module:
181
172
182
173
# import the library
183
174
import urllib
@@ -202,22 +193,18 @@ We can look for which functions are implemented in each module by using the `dir
When we find the function in the module we want to use, we can read about it more using the `help` function,
206
-
inside the Python interpreter:
196
+
When we find the function in the module we want to use, we can read more about it with the `help` function, using the Python interpreter:
207
197
208
198
help(urllib.urlopen)
209
199
210
200
### Writing packages
211
201
212
-
Packages are namespaces which contain multiple packages and modules themselves. They are simply directories,
213
-
but with a twist.
202
+
Packages are namespaces containing multiple packages and modules. They're just directories, but with certain requirements.
214
203
215
-
Each package in Python is a directory which **MUST** contain a special file called `__init__.py`. This file can
216
-
be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same
217
-
way a module can be imported.
204
+
Each package in Python is a directory which **MUST** contain a special file called `__init__.py`. This file, which can be empty, indicates that the directory it's in is a Python package. That way it can be imported the same way as a module.
218
205
219
206
If we create a directory called `foo`, which marks the package name, we can then create a module inside that
220
-
package called `bar`. We also must not forget to add the `__init__.py` file inside the `foo` directory.
207
+
package called `bar`. Then we add the `__init__.py` file inside the `foo` directory.
221
208
222
209
To use the module `bar`, we can import it in two ways:
223
210
@@ -227,11 +214,9 @@ or:
227
214
228
215
from foo import bar
229
216
230
-
In the first method, we must use the `foo` prefix whenever we access the module `bar`. In the second method,
231
-
we don't, because we import the module to our module's namespace.
217
+
In the first example above, we have to use the `foo` prefix whenever we access the module `bar`. In the second example, we don't, because we've imported the module to our module's namespace.
232
218
233
-
The `__init__.py` file can also decide which modules the package exports as the API, while keeping other modules
234
-
internal, by overriding the `__all__` variable, like so:
219
+
The `__init__.py` file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the `__all__` variable like so:
235
220
236
221
__init__.py:
237
222
@@ -240,8 +225,7 @@ internal, by overriding the `__all__` variable, like so:
240
225
Exercise
241
226
--------
242
227
243
-
In this exercise, you will need to print an alphabetically sorted list of all functions in the `re` module,
244
-
which contain the word `find`.
228
+
In this exercise, print an alphabetically sorted list of all the functions in the `re` module containing the word `find`.
0 commit comments