Skip to content

Commit b532fa3

Browse files
authored
Python tutorial revisions (ronreiter#697)
* 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]>
1 parent 2403fd6 commit b532fa3

File tree

1 file changed

+40
-56
lines changed

1 file changed

+40
-56
lines changed

Diff for: tutorials/learnpython.org/en/Modules and Packages.md

+40-56
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ Tutorial
22
--------
33

44
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.
88

99
### Writing modules
1010

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:
1414

15-
- mygame/
15+
mygame/
1616

1717
- mygame/game.py
1818

1919
- mygame/draw.py
2020

2121

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.
2424

2525
Modules are imported from other modules using the `import` command. In this example, the `game.py` script may
2626
look something like this:
@@ -52,23 +52,20 @@ The `draw` module may look something like this:
5252
...
5353

5454
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
5858
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()`.
6060

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.
6462

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.
6865

6966
### Importing module objects to the current namespace
7067

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.
7269

7370
# game.py
7471
# import the draw module
@@ -79,17 +76,14 @@ We may also import the function `draw_game` directly into the main script's name
7976
draw_game(result)
8077

8178

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.
8480

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.
8882

8983

9084
### Importing all objects from a module
9185

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:
9387

9488
# game.py
9589
# import the draw module
@@ -99,16 +93,16 @@ We may also use the `import *` command to import all objects from a specific mod
9993
result = play_game()
10094
draw_game(result)
10195

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.
10498

10599

106100
### Custom import name
107101

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
109103
to use the same name in the rest of the code.
110104

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:
112106

113107

114108
# game.py
@@ -128,11 +122,9 @@ For example, if you have two `draw` modules with slighty different names - you m
128122

129123
### Module initialization
130124

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.
134126

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.
136128
For example:
137129

138130

@@ -155,29 +147,28 @@ For example:
155147

156148
### Extending module load path
157149

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:
161152

162153
PYTHONPATH=/foo python game.py
163154

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
165156
as the local directory.
166157

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:
168159

169160
sys.path.append("/foo")
170161

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+
172164

173165
### Exploring built-in modules
174166

175167
Check out the full list of built-in modules in the Python standard library [here](https://docs.python.org/3/library/).
176168

177169
Two very important functions come in handy when exploring modules in Python - the `dir` and `help` functions.
178170

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:
181172

182173
# import the library
183174
import urllib
@@ -202,22 +193,18 @@ We can look for which functions are implemented in each module by using the `dir
202193
'thishost', 'time', 'toBytes', 'unquote', 'unquote_plus', 'unwrap', 'url2pathname', 'urlcleanup', 'urlencode',
203194
'urlopen', 'urlretrieve']
204195

205-
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:
207197

208198
help(urllib.urlopen)
209199

210200
### Writing packages
211201

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.
214203

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.
218205

219206
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.
221208

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

@@ -227,11 +214,9 @@ or:
227214

228215
from foo import bar
229216

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.
232218

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:
235220

236221
__init__.py:
237222

@@ -240,8 +225,7 @@ internal, by overriding the `__all__` variable, like so:
240225
Exercise
241226
--------
242227

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`.
245229

246230
Tutorial Code
247231
-------------

0 commit comments

Comments
 (0)