Skip to content

Instantly share code, notes, and snippets.

@pekkavaa
Created August 4, 2025 09:22
Show Gist options
  • Save pekkavaa/4a146929e1d2c9b2852e8fe17818a917 to your computer and use it in GitHub Desktop.
Save pekkavaa/4a146929e1d2c9b2852e8fe17818a917 to your computer and use it in GitHub Desktop.
Python dataclass gotcha

Small Python dataclass gotcha

Members not set via the constructor are ignored in equality comparisons. Consider other in the code example below:

from dataclasses import dataclass

@dataclass
class Data:
    value: int
    other = 1  # conceptually not part of a dataclass

a = Data(0)
b = Data(0)
assert a == b, "values are equal"
b.other = 100
assert a == b, "values are still equal!"

print("a:", a)
print("b:", a) # even in printouts "other" is ignored
print("a == b:", a == b)

As you can see both assertion checks pass:

a: Data(value=0)
b: Data(value=0)
a == b: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment