Skip to content

Commit 0a98d68

Browse files
committed
streamline loop (revert if less readable or problematic)
1 parent 0432c2a commit 0a98d68

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Lib/dataclasses.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,12 @@ def _tuple_str(obj_name, fields):
434434

435435

436436
def _tuple_compare_expand(op, fields):
437-
if fields:
438-
for f in fields[:-1]:
439-
yield f' if self.{f.name} != other.{f.name}:'
440-
yield f' return self.{f.name} {op} other.{f.name}'
441-
yield f' return self.{fields[-1].name} {op} other.{fields[-1].name}'
442-
else:
443-
yield f' return {op.endswith("=")}'
437+
op_without_eq = op[0]
438+
op_has_eq = op[-1] == "="
439+
for f in fields:
440+
yield f' if self.{f.name} != other.{f.name}:'
441+
yield f' return self.{f.name} {op_without_eq} other.{f.name}'
442+
yield f' return {op_has_eq}'
444443

445444

446445
class _FuncBuilder:
@@ -1146,8 +1145,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
11461145
# Create a comparison function. If the fields in the object are
11471146
# named 'x' and 'y'.
11481147
# if self.x != other.x:
1149-
# return self.x {op} other.x
1150-
# return self.y {op} other.y
1148+
# return self.x {op[0]} other.x
1149+
# if self.y != other.y:
1150+
# return self.y {op[0]} other.y
1151+
# return {op.endswith("=")}
11511152
self_guard = [' if self is other:', ' return True'] if op.endswith("=") else []
11521153
func_builder.add_fn(name,
11531154
('self', 'other'),

0 commit comments

Comments
 (0)