Skip to content

Commit a8fd02e

Browse files
committed
incomplete inheritance fixes
1 parent 0a01874 commit a8fd02e

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

doc/source/7_inheritance.rst

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ minimal characterisation of a group will suffice.
163163
:caption: A simple implementation of a cyclic group class, and a generic
164164
group element.
165165
:name: cyclic_group
166-
:linenos:
166+
:lineno-start: 7
167167
168168
class Element:
169169
"""An element of the specified group.
@@ -196,7 +196,6 @@ minimal characterisation of a group will suffice.
196196
197197
class CyclicGroup:
198198
"""A cyclic group represented by addition modulo group order."""
199-
200199
def __init__(self, order):
201200
self.order = order
202201
@@ -255,35 +254,32 @@ integer between 0 and 5, an exception is raised.
255254

256255
.. code-block:: ipython3
257256
258-
---------------------------------------------------------------------------
259-
ValueError Traceback (most recent call last)
260-
<ipython-input-4-a5d8472d4486> in <module>
261-
----> 1 C(1.5)
262-
263-
~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py in __call__(self, value)
264-
58 def __call__(self, value):
265-
59 """Create an element of this group."""
266-
---> 60 return Element(self, value)
267-
61
268-
62 def __str__(self):
269-
270-
~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py in __init__(self, group, value)
271-
17
272-
18 def __init__(self, group, value):
273-
---> 19 group._validate(value)
274-
20 self.group = group
275-
21 self.value = value
276-
277-
~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py in _validate(self, value)
278-
46 """Ensure that value is a legitimate element value in this group."""
279-
47 if not (isinstance(value, Integral) and 0 <= value < self.order):
280-
---> 48 raise ValueError("Element value must be an integer"
281-
49 f" in the range [0, {self.order})")
282-
50
283-
284-
ValueError: Element value must be an integer in the range [0, 5)
285-
286-
:numref:`cyclic_group` illustrates :term:`composition`: on line 13
257+
In [4]: C(1.5)
258+
--------------------------------------------------------------------------
259+
ValueError Traceback (most recent call last)
260+
Cell In[4], line 1
261+
----> 1 C(1.5)
262+
263+
File ~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py:56, in CyclicGroup.__call__(self, value)
264+
54 def __call__(self, value):
265+
55 """Create an element of this group."""
266+
---> 56 return Element(self, value)
267+
268+
File ~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py:18, in Element.__init__(self, group, value)
269+
17 def __init__(self, group, value):
270+
---> 18 group._validate(value)
271+
19 self.group = group
272+
20 self.value = value
273+
274+
File ~/docs/principles_of_programming/object-oriented-programming/example_code/groups_basic.py:44, in CyclicGroup._validate(self, value)
275+
42 """Ensure that value is an allowed element value in this group."""
276+
43 if not (isinstance(value, Integral) and 0 <= value < self.order):
277+
---> 44 raise ValueError("Element value must be an integer"
278+
45 f" in the range [0, {self.order})")
279+
280+
ValueError: Element value must be an integer in the range [0, 5)
281+
282+
:numref:`cyclic_group` illustrates :term:`composition`: on line 19
287283
:class:`~example_code.groups_basic.Element` is associated with a group object.
288284
This is a classic *has a* relationship: an element has a group. We might have
289285
attempted to construct this the other way around with groups having elements,
@@ -295,8 +291,8 @@ defined.
295291
This code also demonstrates :term:`delegation`. In order to avoid having to
296292
define different element classes for different groups, the element class does
297293
not in substance implement either value validation, or the group operation.
298-
Instead, at line 12, validation is delegated to the group by calling
299-
:meth:`group._validate` and at line 19 the implementation of the group
294+
Instead, at line 18, validation is delegated to the group by calling
295+
:meth:`group._validate` and at line 25 the implementation of the group
300296
operation is delegated to the group by calling :meth:`self.group.operation`.
301297

302298
General linear groups
@@ -381,7 +377,8 @@ does.
381377

382378
.. code-block:: python3
383379
:caption: Implementation of a base class for a generic group, and
384-
subclasses for the cyclic groups and general linear groups.
380+
subclasses for the cyclic groups and general linear groups. This code
381+
is available in the book repository in :file:`example_code/groups.py`
385382
:name: groups_inheritance
386383
:linenos:
387384
@@ -532,22 +529,22 @@ were to instantiate :class:`Group` itself:
532529

533530
.. code-block:: ipython3
534531
535-
In [1]: from example_code.groups import Group
532+
In [1]: from example_code.groups import Group
536533
537-
In [2]: g = Group(1)
534+
In [2]: g = Group(1)
538535
539-
In [3]: print(g)
540-
---------------------------------------------------------------------------
541-
AttributeError Traceback (most recent call last)
542-
<ipython-input-3-e1cdc681402c> in <module>
543-
----> 1 print(g)
544-
545-
~/docs/principles_of_programming/object-oriented-programming/example_code/groups.py in __str__(self)
546-
61 def __str__(self):
547-
62 """Return a string in the form symbol then group parameter."""
548-
---> 63 return f"{self.symbol}{self.n}"
549-
64
550-
65 def __repr__(self):
536+
In [3]: print(g)
537+
--------------------------------------------------------------------------
538+
AttributeError Traceback (most recent call last)
539+
Cell In[3], line 1
540+
----> 1 print(g)
541+
542+
File ~/docs/principles_of_programming/object-oriented-programming/example_code/groups.py:62, in Group.__str__(self)
543+
60 def __str__(self):
544+
61 """Return a string in the form symbol then group parameter."""
545+
---> 62 return f"{self.symbol}{self.n}"
546+
547+
AttributeError: 'Group' object has no attribute 'symbol'
551548
552549
In fact, :class:`Group` is never supposed to be instantiated, it plays the role
553550
of an :term:`abstract base class`. In other words, its role is to provide

0 commit comments

Comments
 (0)