Skip to content

Commit 168b11e

Browse files
committed
update string representation
1 parent c5b2082 commit 168b11e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

‎doc/source/3_objects.rst

+9
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,15 @@ We can now observe the difference in the result:
476476
In [3]: f
477477
Out[3]: Polynomial((1, 2, 0, 4, 5))
478478
479+
When using :ref:`f-strings <python:tut-f-strings>`, the :func:`repr` of a an
480+
object can be inserted instead of the :class:`str` by using the ``!r`` modifier.
481+
For example, we could just as well have written the method above as:
482+
483+
.. code-block:: python3
484+
485+
def __repr__(self):
486+
return f"{type(self).__name__}({self.coefficients!r})"
487+
479488
.. _object_equality:
480489

481490
Object equality

‎doc/source/7_inheritance.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ minimal characterisation of a group will suffice.
191191
192192
def __repr__(self):
193193
"""Return the canonical string representation of the element."""
194-
return f"{type(self).__name__}" \
195-
f"({repr(self.group)}, {repr(self.value)})"
194+
return f"{type(self).__name__}{self.group, self.value!r}"
196195
197196
198197
class CyclicGroup:

‎example_code/groups.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Element:
1717
value:
1818
The value of this entity. Valid values depend on the group.
1919
"""
20+
2021
def __init__(self, group, value):
2122
group._validate(value)
2223
self.group = group
@@ -34,8 +35,7 @@ def __str__(self):
3435

3536
def __repr__(self):
3637
"""Return the canonical string representation of the element."""
37-
return f"{type(self).__name__}" \
38-
f"({repr(self.group), repr(self.value)})"
38+
return f"{type(self).__name__}{self.group, self.value!r}"
3939

4040

4141
class Group:
@@ -49,6 +49,7 @@ class Group:
4949
The primary group parameter, such as order or degree. The
5050
precise meaning of n changes from subclass to subclass.
5151
"""
52+
5253
def __init__(self, n):
5354
self.n = n
5455

@@ -67,6 +68,7 @@ def __repr__(self):
6768

6869
class CyclicGroup(Group):
6970
"""A cyclic group represented by integer addition modulo n."""
71+
7072
symbol = "C"
7173

7274
def _validate(self, value):

0 commit comments

Comments
 (0)