-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added numbers for clarity. Added not implemented example.
- Loading branch information
Showing
15 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions
8
2024/weird_things/integer_caching.py → 2024/weird_things/1_integer_caching.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.