@@ -60,53 +60,53 @@ include:
60
60
Modules
61
61
-------
62
62
63
- Python modules are one of the main abstraction layer available and probably the
63
+ Python modules are one of the main abstraction layers available and probably the
64
64
most natural one. Abstraction layers allow separating code into parts holding
65
- related data and functionalities .
65
+ related data and functionality .
66
66
67
67
For example, a layer of a project can handle interfacing with user actions,
68
68
while another would handle low-level manipulation of data. The most natural way
69
- to separate these two layers is to regroup all interfacing functionalities
69
+ to separate these two layers is to regroup all interfacing functionality
70
70
in one file, and all low-level operations in another file. In this case,
71
- the interface file need to import the low-level file. This is done with the
71
+ the interface file needs to import the low-level file. This is done with the
72
72
`import ` and `from ... import ` statements.
73
73
74
- As soon as you use `import ` statements you use modules, either builtin modules
75
- such as `os ` and `sys `, or third-party modules you have installed in your
76
- environment, or project's internal modules.
74
+ As soon as you use `import ` statements you use modules. These can be either built-in
75
+ modules such as `os ` and `sys `, third-party modules you have installed in your
76
+ environment, or your project's internal modules.
77
77
78
78
Nothing special is required for a Python file to be a module, but the import
79
- mechanism need to be understood in order to use this concept properly and avoid
79
+ mechanism needs to be understood in order to use this concept properly and avoid
80
80
some issues.
81
81
82
82
Concretely, the `import modu ` statement will look for the proper file, which is
83
83
`modu.py ` in the same directory as the caller if it exists. If it is not
84
- found, the Python interpreter with search for `modu.py ` in the "path"
84
+ found, the Python interpreter will search for `modu.py ` in the "path"
85
85
recursively and raise an ImportError exception if it is not found.
86
86
87
87
Once `modu.py ` is found, the Python interpreter will execute the module in an
88
88
isolated scope. Any top-level statement in `modu.py ` will be executed,
89
- including other imports if any. Function and classes definitions are stored in
89
+ including other imports if any. Function and class definitions are stored in
90
90
the module's dictionary.
91
91
92
- Then modules variables, functions and classes will be available to the caller
92
+ Then, the module's variables, functions, and classes will be available to the caller
93
93
through the module's namespace, a central concept in programming that is
94
94
particularly helpful and powerful in Python.
95
95
96
- In many languages, a `include file ` directive is used by the preprocessor to
97
- take all code found in the file and 'copy' it in the caller's code. It is
96
+ In many languages, an `include file ` directive is used by the preprocessor to
97
+ take all code found in the file and 'copy' it into the caller's code. It is
98
98
different in Python: the included code is isolated in a module namespace, which
99
99
means that you generally don't have to worry that the included code could have
100
- unwanted effect, eg override an existing function with the same name.
100
+ unwanted effects, e.g. override an existing function with the same name.
101
101
102
102
It is possible to simulate the more standard behavior by using a special syntax
103
103
of the import statement: `from modu import * `. This is generally considered bad
104
- practice, **using import * makes code harder to read and dependencies less
105
- compartimented **.
104
+ practice. **Using ` import *` makes code harder to read and makes dependencies less
105
+ compartmentalized **.
106
106
107
107
Using `from modu import func ` is a way to pinpoint the function you want to
108
- import and put it is the global namespace. While much less harmful than `import
109
- * ` because it shows explicitely what is imported in the global namespace, it's
108
+ import and put it in the global namespace. While much less harmful than `import
109
+ * ` because it shows explicitly what is imported in the global namespace, its
110
110
advantage over a simpler `import modu ` is only that it will save some typing.
111
111
112
112
**Very bad **
@@ -134,13 +134,13 @@ advantage over a simpler `import modu` is only that it will save some typing.
134
134
[... ]
135
135
x = modu.sqrt(4 ) # sqrt is visibly part of modu's namespace
136
136
137
- As said in the section about style, readability is one of the main feature of
137
+ As said in the section about style, readability is one of the main features of
138
138
Python. Readability means to avoid useless boilerplate text and clutter,
139
139
therefore some efforts are spent trying to achieve a certain level of brevity.
140
- But terseness and obscurity are the limits where brevity should stop: being
141
- able to tell immediately from where comes a class or a function, as in the
142
- `modu.func ` idiom, improves greatly code readability and understandability in
143
- most cases but the simplest single file projects.
140
+ But terseness and obscurity are the limits where brevity should stop. Being
141
+ able to tell immediately where a class or function comes from , as in the
142
+ `modu.func ` idiom, greatly improves code readability and understandability in
143
+ all but the simplest single file projects.
144
144
145
145
146
146
Packages
0 commit comments