summaryrefslogtreecommitdiffstats
path: root/Lib/dataclasses.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2023-05-30 16:35:30 (GMT)
committerGitHub <noreply@github.com>2023-05-30 16:35:30 (GMT)
commit18cfc1eea569f0ce72ad403840c0e6cc5f81e1c2 (patch)
tree6c22d1e19cd837879770365203b29e1c8a63ed76 /Lib/dataclasses.py
parentc8c1e73d95d5dcd7a2c8d7c10cfafc3fe5a9377e (diff)
downloadcpython-18cfc1eea569f0ce72ad403840c0e6cc5f81e1c2.zip
cpython-18cfc1eea569f0ce72ad403840c0e6cc5f81e1c2.tar.gz
cpython-18cfc1eea569f0ce72ad403840c0e6cc5f81e1c2.tar.bz2
Small speedup for dataclass __eq__ and __repr__ (#104904)
Faster __repr__ with str.__add__ moved inside the f-string. For __eq__ comp;are field by field instead of building temporary tuples. Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r--Lib/dataclasses.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 3eacba8..e766a7b 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -627,7 +627,7 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
def _repr_fn(fields, globals):
fn = _create_fn('__repr__',
('self',),
- ['return self.__class__.__qualname__ + f"(' +
+ ['return f"{self.__class__.__qualname__}(' +
', '.join([f"{f.name}={{self.{f.name}!r}}"
for f in fields]) +
')"'],
@@ -1085,13 +1085,17 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
if eq:
# Create __eq__ method. There's no need for a __ne__ method,
# since python will call __eq__ and negate it.
- flds = [f for f in field_list if f.compare]
- self_tuple = _tuple_str('self', flds)
- other_tuple = _tuple_str('other', flds)
- _set_new_attribute(cls, '__eq__',
- _cmp_fn('__eq__', '==',
- self_tuple, other_tuple,
- globals=globals))
+ cmp_fields = (field for field in field_list if field.compare)
+ terms = [f'self.{field.name}==other.{field.name}' for field in cmp_fields]
+ field_comparisons = ' and '.join(terms) or 'True'
+ body = [f'if other.__class__ is self.__class__:',
+ f' return {field_comparisons}',
+ f'return NotImplemented']
+ func = _create_fn('__eq__',
+ ('self', 'other'),
+ body,
+ globals=globals)
+ _set_new_attribute(cls, '__eq__', func)
if order:
# Create and set the ordering methods.