Skip to content

Commit dd83708

Browse files
committed
🎨 Rearrange namespaces
1 parent b46e004 commit dd83708

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

docs/functions/variables.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Variables
22
=========
33

4+
.. _local_variables:
5+
46
Local variables
57
---------------
68

@@ -38,6 +40,8 @@ such as ``f = 1``, are local to the function:
3840
n
3941
NameError: name 'n' is not defined
4042
43+
.. _global_variables:
44+
4145
Global variables
4246
----------------
4347

@@ -75,6 +79,8 @@ true for ``y``; the local variable ``y`` inside ``my_func`` initially refers to
7579
the same value as the variable ``y`` outside ``my_func``, but the assignment
7680
causes ``y`` to refer to a new value that is local to the ``my_func`` function.
7781

82+
.. _nonlocal_variables:
83+
7884
Non-local variables
7985
-------------------
8086

docs/oop/form_ns.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def circumferences(cls):
6363
return csum
6464

6565
def namespaces(self):
66+
print("Builtin namespace:", dir(__builtins__))
6667
print("Global namespace:", list(globals().keys()))
6768
print("Superclass namespace:", dir(Form))
6869
print("Class namespace:", dir(Circle))

docs/oop/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ be the beginning of a simple shapes module for a drawing program.
5151
coherent
5252
private
5353
property
54-
namespaces
5554
types
55+
namespaces
5656
dataclasses
5757
design/index

docs/oop/namespaces.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Namespaces
22
==========
33

4+
A namespace is a collection of currently defined symbolic names and information
5+
about an object. You can think of a namespace as a dictionary in which the keys
6+
are the object names and the values are the objects themselves. Each of these
7+
key-value pairs assigns a name to the corresponding object.
8+
9+
Namespaces are one honking great idea – let’s do more of those!
10+
11+
– `The Zen of Python <https://peps.python.org/pep-0020/>`_, by Tim Peters
12+
13+
Python now uses namespaces extensively. We have already learnt about some of
14+
them in :doc:`function variables <../functions/variables>`: :ref:`local
15+
<local_variables>`, :ref:`global <global_variables>` and :ref:`non-local
16+
variables <nonlocal_variables>`.
17+
418
If you are in the method of a class, you have direct access
519

620
#. to the **local namespace** with the parameters and variables declared in this
@@ -21,14 +35,15 @@ You can get an overview of the methods that are available in a namespace with
2135
.. literalinclude:: form_ns.py
2236
:language: python
2337
:linenos:
24-
:lines: 65-70
38+
:lines: 65-71
2539
:lineno-start: 65
2640

2741
.. code-block:: pycon
2842
2943
>>> import form_ns
3044
>>> c1 = form_ns.Circle()
3145
>>> c1.namespaces()
46+
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'PythonFinalizationError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '_IncompleteInputError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
3247
Global namespace: ['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__file__', '__cached__', '__builtins__', 'Form', 'Square', 'Circle']
3348
Superclass namespace: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'move']
3449
Class namespace: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'circles', 'circumference', 'circumferences', 'diameter', 'instance_variables', 'move', 'namespaces', 'pi']
@@ -64,8 +79,8 @@ You can now analyse the namespace of the instance with the method
6479
.. literalinclude:: form_ns.py
6580
:language: python
6681
:linenos:
67-
:lines: 72-
68-
:lineno-start: 72
82+
:lines: 73-
83+
:lineno-start: 73
6984

7085
.. code-block:: pycon
7186

0 commit comments

Comments
 (0)