diff --git a/2024/weird_things/return_after.py b/2024/weird_things/10_return_after.py similarity index 100% rename from 2024/weird_things/return_after.py rename to 2024/weird_things/10_return_after.py diff --git a/2024/weird_things/space_invader_operator.py b/2024/weird_things/11_space_invader_operator.py similarity index 100% rename from 2024/weird_things/space_invader_operator.py rename to 2024/weird_things/11_space_invader_operator.py diff --git a/2024/weird_things/function_attribute_mutation.py b/2024/weird_things/12_function_attribute_mutation.py similarity index 100% rename from 2024/weird_things/function_attribute_mutation.py rename to 2024/weird_things/12_function_attribute_mutation.py diff --git a/2024/weird_things/class_members.py b/2024/weird_things/13_class_members.py similarity index 100% rename from 2024/weird_things/class_members.py rename to 2024/weird_things/13_class_members.py diff --git a/2024/weird_things/class_members_dataclass.py b/2024/weird_things/13_class_members_dataclass.py similarity index 100% rename from 2024/weird_things/class_members_dataclass.py rename to 2024/weird_things/13_class_members_dataclass.py diff --git a/2024/weird_things/integer_caching.py b/2024/weird_things/1_integer_caching.py similarity index 63% rename from 2024/weird_things/integer_caching.py rename to 2024/weird_things/1_integer_caching.py index 8469d1b..b1e0886 100644 --- a/2024/weird_things/integer_caching.py +++ b/2024/weird_things/1_integer_caching.py @@ -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 diff --git a/2024/weird_things/default_empty_list.py b/2024/weird_things/2_default_empty_list.py similarity index 100% rename from 2024/weird_things/default_empty_list.py rename to 2024/weird_things/2_default_empty_list.py diff --git a/2024/weird_things/3_not_implemented.py b/2024/weird_things/3_not_implemented.py new file mode 100644 index 0000000..5916899 --- /dev/null +++ b/2024/weird_things/3_not_implemented.py @@ -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() diff --git a/2024/weird_things/mutating_immutable_struct.md b/2024/weird_things/4_mutating_immutable_struct.md similarity index 100% rename from 2024/weird_things/mutating_immutable_struct.md rename to 2024/weird_things/4_mutating_immutable_struct.md diff --git a/2024/weird_things/booleans_subclassing_integers.py b/2024/weird_things/5_booleans_subclassing_integers.py similarity index 100% rename from 2024/weird_things/booleans_subclassing_integers.py rename to 2024/weird_things/5_booleans_subclassing_integers.py diff --git a/2024/weird_things/reference_inconsistency.py b/2024/weird_things/6_reference_inconsistency.py similarity index 100% rename from 2024/weird_things/reference_inconsistency.py rename to 2024/weird_things/6_reference_inconsistency.py diff --git a/2024/weird_things/variable_global_scope_a.py b/2024/weird_things/7_variable_global_scope_a.py similarity index 100% rename from 2024/weird_things/variable_global_scope_a.py rename to 2024/weird_things/7_variable_global_scope_a.py diff --git a/2024/weird_things/variable_global_scope_b.py b/2024/weird_things/7_variable_global_scope_b.py similarity index 100% rename from 2024/weird_things/variable_global_scope_b.py rename to 2024/weird_things/7_variable_global_scope_b.py diff --git a/2024/weird_things/for_else_clause.py b/2024/weird_things/8_for_else_clause.py similarity index 100% rename from 2024/weird_things/for_else_clause.py rename to 2024/weird_things/8_for_else_clause.py diff --git a/2024/weird_things/delayed_printing.py b/2024/weird_things/9_delayed_printing.py similarity index 100% rename from 2024/weird_things/delayed_printing.py rename to 2024/weird_things/9_delayed_printing.py