Skip to content

Commit

Permalink
Added numbers for clarity. Added not implemented example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjan Egges committed Aug 23, 2024
1 parent 89fd992 commit 4b3e943
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def main() -> None:
a = 256
b = 256
a = int("256")
b = int("256")
print(a is b) # Output: True

c = 257
d = 257
c = int("257")
d = int("257")
print(c is d) # Output: False


Expand Down
File renamed without changes.
44 changes: 44 additions & 0 deletions 2024/weird_things/3_not_implemented.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from dataclasses import dataclass


class Shape:
def area(self) -> float:
raise NotImplementedError("Subclasses must implement this method")


@dataclass
class Rectangle(Shape):
width: float
height: float

def area(self) -> float:
return self.width * self.height

def __eq__(self, other: object) -> bool:
if not isinstance(other, Rectangle):
return NotImplemented # Indicates the comparison is not implemented for this type
return self.area() == other.area()


def main() -> None:
# Testing NotImplementedError
try:
# This will raise NotImplementedError since 'area' is not implemented
c = Shape()
c.area()
except NotImplementedError as e:
print(e) # Output: Subclasses must implement this method

# Testing NotImplemented
r1 = Rectangle(2, 3)
r2 = Rectangle(2, 3)
r3 = "not a rectangle"

print(r1 == r2) # Output: True, since areas are the same
print(
r1 == r3
) # Output: False, NotImplemented is returned and Python falls back to other methods of comparison


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.

0 comments on commit 4b3e943

Please sign in to comment.