Skip to content

Commit

Permalink
Merge pull request #28 from DavidCEllis/faster_eq
Browse files Browse the repository at this point in the history
Faster `__eq__` comparison
  • Loading branch information
DavidCEllis authored Aug 31, 2024
2 parents cd39f4c + cbe9695 commit 7280965
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ducktools/classbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,17 @@ def eq_generator(cls, funcname="__eq__"):
]

if field_names:
selfvals = ",".join(f"self.{name}" for name in field_names)
othervals = ",".join(f"other.{name}" for name in field_names)
instance_comparison = f"({selfvals},) == ({othervals},)"
instance_comparison = "\n and ".join(
f"self.{name} == other.{name}" for name in field_names
)
else:
instance_comparison = "True"

code = (
f"def {funcname}(self, other):\n"
f" return {instance_comparison} if {class_comparison} else NotImplemented\n"
f" return (\n"
f" {instance_comparison}\n"
f" ) if {class_comparison} else NotImplemented\n"
)
globs = {}

Expand Down

0 comments on commit 7280965

Please sign in to comment.