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
{{ message }}
Repository navigation
Commit 9e7428b
Browse filesBrowse the repository at this point in the historyBrowse files
.. centered:: **Always use four spaces -- really!** (`PEP 8 <https://www.python.org/dev/peps/pep-0008/>`_)
63
61
64
62
.. note::
65
63
If you *do* use tabs (and really, don't do that!) python interprets them as the equivalent of *eight* spaces. Text editors can display tabs as any number of spaces, and most modern editors default to four -- so this can be *very* confusing! so again:
66
64
67
-
**Never mix tabs and spaces in Python code**
65
+
.. centered::**Never mix tabs and spaces in Python code**
68
66
69
67
70
68
Spaces Elsewhere
@@ -86,7 +84,7 @@ But you should strive for proper style. Isn't this easier to read?
86
84
x = (3*4) + (12/ func(x, y, z))
87
85
88
86
89
-
**ReadPEP 8and install a linter in your editor.**
87
+
.. centered:: **Read** `**PEP 8** <https://www.python.org/dev/peps/pep-0008/>`_ **and install a linter in your editor.**
90
88
91
89
92
90
Modules and Packages
@@ -98,10 +96,10 @@ Python is all about *namespaces* -- the "dots"
98
96
99
97
The "dot" indicates that you are looking for a name in the *namespace* of the given object. It could be:
100
98
101
-
* a name in a module
102
-
* a module in a package
103
-
* an attribute of an object
104
-
* a method of an object
99
+
* A name in a module
100
+
* A module in a package
101
+
* An attribute of an object
102
+
* A method of an object
105
103
106
104
The only way to know is to know what type of object the name refers to. But in all cases, it is looking up a name in the namespace of the object.
107
105
@@ -120,15 +118,69 @@ So you can think of the files you write that end in ``.py`` as modules.
120
118
121
119
When a module is imported, the code in that file is run, and any names defined in that file are now available in the module namespace.
122
120
121
+
For a really simple example, if you have the following in the ``trivial.py`` file:
122
+
123
+
.. code-block:: python
124
+
:linenos:
125
+
126
+
x =1
127
+
y =2
128
+
129
+
defdo_nothing(a, b, c):
130
+
print("do_nothing was called with:", a, b, c)
131
+
132
+
print("at the end of the trivial module")
133
+
134
+
What do you think happens when you import that module? What will get printed?
135
+
136
+
What names will be defined in that module?
137
+
138
+
How would you access those names?
139
+
140
+
Before running this code, think about it a bit. Recall what happens when you import a module:
141
+
142
+
* The code is run in the module, top to bottom.
143
+
* The names defined in the module (its global namespace) are made available in the modules namespace.
144
+
145
+
So: Lines 1-2 assign two names, ``x`` and ``y``. lines 4-5 define a function, named ``do_nothing``. Line 7 prints something.
146
+
147
+
So: when run, there are three names defined, and one print function run.
148
+
149
+
Now try it:
150
+
151
+
.. code-block:: python
152
+
153
+
>>>import trivial
154
+
at the end of the trivial module
155
+
156
+
yes, we got that print function run.
157
+
158
+
Let's see if the names are there:
159
+
160
+
.. code-block:: python
161
+
162
+
>>> trivial.x
163
+
1
164
+
>>> trivial.y
165
+
2
166
+
167
+
.. code-block:: python
168
+
169
+
170
+
>>> trivial.do_nothing(3,4,5)
171
+
do_nothing was called with: 345
172
+
173
+
yes, there are, in the ``trivial`` namespace.
174
+
123
175
124
176
Packages
125
177
--------
126
178
127
179
A package is a module with other modules in it.
128
180
129
-
On a filesystem, this is represented as a directory that contains one or more ``.py`` files, one of which **must** be called ``__init__.py``.
181
+
On a filesystem, this is represented as a directory that contains one or more ``.py`` files, one of which **must** be called ``__init__.py``. The ``__init__.py`` file can be empty (and often is) -- but it must be there.
130
182
131
-
When you have a package, you can import only the package, or any of the modules inside it. When a package is imported, the code in the ``__init__.py`` file is run, and any names defined in that file are available in the *package namespace*.
183
+
When there is a package available, you can import only the package, or any of the modules inside it. When a package is imported, the code in the ``__init__.py`` file is run, and any names defined in that file are available in the *package namespace*.
132
184
133
185
Here we define about the simplest package possible:
134
186
@@ -181,7 +233,7 @@ What about the module?
181
233
182
234
AttributeError: module 'my_package' has no attribute 'a_module'
183
235
184
-
the a_module name does not exist. It must be imported explicitly:
236
+
the ``a_module`` name does not exist. It must be imported explicitly:
185
237
186
238
.. code-block:: ipython
187
239
@@ -206,6 +258,7 @@ Note that you can also put a package inside a package. So you can create arbitra
206
258
207
259
So don't overdo it -- only go as deep as you really need to to keep your code organized.
208
260
261
+
209
262
Importing modules
210
263
-----------------
211
264
@@ -236,18 +289,22 @@ If you want only a few names in a module, and don't want to type the module name
236
289
237
290
This brings only the names specified (``this``, ``that``) into the global namespace. All the code in the module is run, but the module's name is not available. But the explicitly imported names are directly available.
238
291
292
+
Sometimes you want the entire module, but maybe not want to type its entire name eadh time you use. So you can rename a module when you import it. (you may also want to do this if a module has the same name as a variable you want to use...)
293
+
239
294
.. code-block:: python
240
295
241
296
import modulename as a_new_name
242
297
243
-
This imports the module, and gives it a new name in the global namespace. This is done to avoid a name conflict, or to give the module a shorter name. For example, the numpy module is usually imported as:
298
+
This imports the module, and gives it a new name in the global namespace. For example, the numpy package is usually imported as:
244
299
245
300
.. code-block:: python
246
301
247
302
import numpy as np
248
303
249
304
Because numpy has a LOT of names, some of which may conflict with builtins or other modules, and users want to be able to reference them without too much typing.
250
305
306
+
You can also import a name within a module and rename it at the same time:
307
+
251
308
.. code-block:: python
252
309
253
310
from modulename import this as that
@@ -282,6 +339,7 @@ You can play with some of this with the standard library:
282
339
In [8]: cosine(1.2)
283
340
Out[8]: 0.3623577544766736
284
341
342
+
285
343
My rules of thumb
286
344
-----------------
287
345
@@ -308,15 +366,15 @@ Or import it with a nice short name:
308
366
import \* ?
309
367
-----------
310
368
311
-
**Warning:**
369
+
.. centered:: **Warning:**
312
370
313
371
You can also import all the names in a module with:
314
372
315
373
.. code-block:: python
316
374
317
375
from modulename import*
318
376
319
-
But this leads to name conflicts, and a cluttered namespace. It is NOT recommended practice.
377
+
But this leads to name conflicts, and a cluttered namespace. It is NOT recommended practice anymore.
And :ref:`packaging` goes into more detail about creating (and distributing!) your own package.
338
396
397
+
339
398
What does ``import`` actually do?
340
399
---------------------------------
341
400
342
-
When you import a module, or a symbol from a module, the Python code is *compiled* to **bytecode**.
401
+
When you import a module, or a symbol from a module, the Python code is *compiled* to *bytecode*.
343
402
344
403
The result is a ``module.pyc`` file.
345
404
346
-
Then after compiling, all the code in the module is run **at the module scope**.
405
+
Then after compiling, all the code in the module is run **at the module scope** -- that is, in the namespace of the module.
347
406
348
-
For this reason, it is good to avoid module-scope statements that have global side-effects.
407
+
For this reason, it is good to avoid module-scope statements that have global side-effects. This includes things as simple as a ``print()`` -- it will only print the first time the module is imported.
349
408
350
409
351
410
Re-import
352
411
----------
353
412
354
-
The code in a module is NOT re-run when imported again. This makes it efficient to import the same module multiple places in a program. But it means that if you change the code in a module after importing it, that change will not be reflected when it is imported again.
413
+
The code in a module is *not* re-run when imported again. This makes it efficient to import the same module multiple places in a program. But it means that if you change the code in a module after importing it, that change will not be reflected when it is imported again.
355
414
356
415
If you DO want a change to be reflected, you can explicitly reload a module:
You can see that when ``mod2`` changed the value of ``mod1.x``, that changed the value everywhere that ``mod1`` is imported. You want to be very careful about this.
428
487
429
488
If you are writing ``mod2.py``, and did not write ``mod3`` (or wrote it long enough ago that you don't remember its details), you might be very surprised that a value in ``mod1`` changes simply because you imported ``mod3``. This is known as a "side effect", and you generally want to avoid them!
0 commit comments