Skip to content

Compare

compare floats

1
2
3
4
5
6
7
if __name__ == '__main__':
    a: float = 1.23
    b: float = 1.230000000001

    print("numbers are equal") if (a == b) else print("numbers are different")
    result: bool = abs(a - b) < 1e-4
    print(result)

compare strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if __name__ == '__main__':
    print("cadwork" == "cadwork")
    print("Cadwork" < "cadwork")
    print("Cadwork" > "cadwork")
    print("cadwork" != "cadwork")

#output
# True
# True
# False
# False

compare objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import dataclasses

# value class
@dataclasses.dataclass()
class Address:
    street: str
    number: int
    zipcode: int

    def __eq__(self, other):
        if not isinstance(other, Address):
            return False
        return self.street == other.street \
            and self.number == other.number \
            and self.zipcode == other.zipcode

# entity class
class Person:
    def __init__(self, name, passport_id, address):
        self.name: str = name
        self.address: Address = address
        self.passport_id: int = passport_id

    def __eq__(self, other):
        if not isinstance(other, Person):
            return False
        return self.passport_id == other.passport_id

    def __hash__(self):
        return hash(self.passport_id)


if __name__ == '__main__':
    address1 = Address(street="ThisStreet", number=20, zipcode=8084)
    address2 = Address(street="OtherStreet", number=204, zipcode=9000)

    print(address1 == address2)

    person1 = Person(name="John", passport_id=123456, address=address1)
    person2 = Person(name="John", passport_id=123456, address=address2)

    print(person1 == person2)
    print(person1.__hash__() == person2.__hash__())