@@ -11,6 +11,7 @@ What is a module?
1111
1212A module is a file that contains code. It defines a group of Python functions or
1313other objects, and the name of the module is derived from the name of the file.
14+
1415Modules usually contain Python source code [# ]_, group related Python objects
1516together and help to avoid naming conflicts. You can write a module called
1617``mymodule `` for your programme that defines a function called ``my_func ``. In
@@ -20,9 +21,25 @@ something different from your ``my_func`` function. Without modules, it would be
2021impossible to use two different functions with the same name. With modules, you
2122can refer to the functions ``mymodule.my_func `` and ``othermodule.my_func `` in
2223your main programme. Using the module names ensures that the two ``my_func ``
23- functions are not confused, as Python uses so-called namespaces. A namespace is
24- essentially a dictionary of names for the functions, classes, modules,
25- :abbr: `etc. ( et cetera ) ` available there.
24+ functions are not confused, as Python uses so-called :doc: `../oop/namespaces `. A
25+ namespace is essentially a :doc: `dictionary <../types/dicts >` of names for the
26+ functions, classes, modules, :abbr: `etc. ( et cetera ) ` available there.
27+
28+ Namespaces are one honking great idea – let’s do more of those!
29+
30+ – `The Zen of Python <https://peps.python.org/pep-0020/ >`_, by Tim Peters
31+
32+ .. code-block :: pycon
33+
34+ >>> import cmath, math
35+ >>> math.sin(34)
36+ 0.5290826861200238
37+ >>> cmath.sin(34)
38+ (0.5290826861200238-0j)
39+
40+ .. warning ::
41+ However, an import using wildcards negates this separation of namespaces:
42+ ``import * `` carries over every name from one module to another.
2643
2744Modules are also used to make Python itself more manageable. Most of Python’s
2845standard functions are not integrated into the core of the language, but are
@@ -179,6 +196,77 @@ In addition, a help option ``-h`` or ``--help`` is automatically generated:
179196 -f FILENAME, --file FILENAME
180197 read data from the file
181198
199+ Declaring a public API with ``__all__ ``
200+ ---------------------------------------
201+
202+ You can use ``__all__ `` to expose a public API for your module, which determines
203+ what is imported when you use :samp: `from { MODULE } import * `. Let’s assume your
204+ module looks like this:
205+
206+ .. literalinclude :: perimeter.py
207+ :caption: perimeter.py
208+
209+ You can then use this module as follows:
210+
211+ .. code-block :: pycon
212+
213+ >>> from perimeter import *
214+ >>> circle_perimeter(3)
215+ 9.42477796076938
216+ >>> square_perimeter(3)
217+ 12
218+ >>> square_length(16)
219+ 4.0
220+
221+ However, you cannot use the ``pi `` variable from the module:
222+
223+ .. code-block :: pycon
224+
225+ >>> pi
226+ Traceback (most recent call last):
227+ File "<python-input-1>", line 1, in <module>
228+ pi
229+ NameError: name 'pi' is not defined
230+
231+ Even with :py:func: `dir `, you’ll still only get those two functions from
232+ ``__all__ ``:
233+
234+ .. code-block :: pycon
235+
236+ >>> dir()
237+ ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'circle_perimeter', 'square_length', 'square_perimeter']
238+
239+ The advantage of this is that internal helper functions and variables are
240+ encapsulated and are not accidentally exported. Without ``__all__ ``, any name
241+ not beginning with an underscore would also have been imported, including ``pi ``
242+ and the ``sqrt `` function, which we imported from :py:mod: `math `.
243+
244+ The :py:func: `help ` function also reads from ``__all__ `` and does not document
245+ everything in the module; the internal variable ``pi `` and ``sqrt ``, which does
246+ not even originate from us, are not displayed:
247+
248+ .. code-block :: pycon
249+
250+ >>> import perimeter
251+ >>> help(perimeter)
252+
253+ .. code-block :: text
254+
255+ Help on module perimeter:
256+
257+ NAME
258+ perimeter
259+
260+ FUNCTIONS
261+ circle_perimeter(diameter)
262+
263+ square_length(area)
264+
265+ square_perimeter(length)
266+
267+ DATA
268+ __all__ = ['circle_perimeter', 'square_length', 'square_perimeter']
269+
182270 Checks
183271------
184272
0 commit comments