From b52ed4692124ff482c3bf24ad517b81eb13bad03 Mon Sep 17 00:00:00 2001 From: Arjan Egges Date: Fri, 23 Aug 2024 16:19:17 +0200 Subject: [PATCH] Added numbers for clarity. Added not implemented example. --- .../{return_after.py => 10_return_after.py} | 0 ...erator.py => 11_space_invader_operator.py} | 0 ...n.py => 12_function_attribute_mutation.py} | 0 .../{class_members.py => 13_class_members.py} | 0 ...class.py => 13_class_members_dataclass.py} | 0 ...nteger_caching.py => 1_integer_caching.py} | 8 ++-- ..._empty_list.py => 2_default_empty_list.py} | 0 2024/weird_things/3_not_implemented.py | 44 +++++++++++++++++++ ...ruct.md => 4_mutating_immutable_struct.md} | 0 ....py => 5_booleans_subclassing_integers.py} | 0 ...stency.py => 6_reference_inconsistency.py} | 0 ...cope_a.py => 7_variable_global_scope_a.py} | 0 ...cope_b.py => 7_variable_global_scope_b.py} | 0 ...or_else_clause.py => 8_for_else_clause.py} | 0 ...ayed_printing.py => 9_delayed_printing.py} | 0 15 files changed, 48 insertions(+), 4 deletions(-) rename 2024/weird_things/{return_after.py => 10_return_after.py} (100%) rename 2024/weird_things/{space_invader_operator.py => 11_space_invader_operator.py} (100%) rename 2024/weird_things/{function_attribute_mutation.py => 12_function_attribute_mutation.py} (100%) rename 2024/weird_things/{class_members.py => 13_class_members.py} (100%) rename 2024/weird_things/{class_members_dataclass.py => 13_class_members_dataclass.py} (100%) rename 2024/weird_things/{integer_caching.py => 1_integer_caching.py} (63%) rename 2024/weird_things/{default_empty_list.py => 2_default_empty_list.py} (100%) create mode 100644 2024/weird_things/3_not_implemented.py rename 2024/weird_things/{mutating_immutable_struct.md => 4_mutating_immutable_struct.md} (100%) rename 2024/weird_things/{booleans_subclassing_integers.py => 5_booleans_subclassing_integers.py} (100%) rename 2024/weird_things/{reference_inconsistency.py => 6_reference_inconsistency.py} (100%) rename 2024/weird_things/{variable_global_scope_a.py => 7_variable_global_scope_a.py} (100%) rename 2024/weird_things/{variable_global_scope_b.py => 7_variable_global_scope_b.py} (100%) rename 2024/weird_things/{for_else_clause.py => 8_for_else_clause.py} (100%) rename 2024/weird_things/{delayed_printing.py => 9_delayed_printing.py} (100%) 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