Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
657 B
5
Indexable
Never
class A:
    def __init__(self, value: int) -> None:
        self.a = value

    def __str__(self) -> str:
        return f"id:{id(self)}, a:{self.a}"

    def __repr__(self) -> str:
        return f"A(id:{id(self)}, a:{self.a})"

    def __hash__(self) -> int:
        return self.a

    def __eq__(self, other) -> bool:
        return self.a == other.a

a: A = A(1)
b: A = A(2)

d: dict[A, int] = { a:1, b:2 }

print(a)
print(b)
print("eq", a == b)
print("hash", hash(a) == hash(b))

print(d)

for key in d.keys():
    if key.a == 1:
        key.a = 2

print(a)
print(b)
print("eq", a == b)
print("hash", hash(a) == hash(b))

print(d)
print({ a:1, b:2 })
Leave a Comment