Skip to content

Commit

Permalink
Fix test in Object-Oriented Example 4 (#262)
Browse files Browse the repository at this point in the history
* Add __eq__ validation in Example 4
* Add a comment to explain the rationale behind __closure__ testing

---------
Co-authored-by: edoardob90 <[email protected]>
  • Loading branch information
despadam authored Nov 18, 2024
1 parent 9240a6d commit 656d3d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
24 changes: 12 additions & 12 deletions object_oriented_programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
" - [Example 1](#Example-1)\n",
" - [Properties and methods](#Properties-and-methods)\n",
" - [Example 2](#Example-2)\n",
" - [Python's *special methods*](#Python's-*special-methods*)\n",
" - [Python's special methods](#Python's-special-methods)\n",
" - [`__str__` and `__repr__`](#__str__-and-__repr__)\n",
" - [Example 3](#Example-3)\n",
" - [Comparison methods](#Comparison-methods)\n",
Expand Down Expand Up @@ -220,7 +220,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -357,7 +357,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand All @@ -367,7 +367,7 @@
"tags": []
},
"source": [
"# Python's *special methods*"
"# Python's special methods"
]
},
{
Expand Down Expand Up @@ -684,7 +684,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -974,7 +974,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -1429,7 +1429,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -1476,11 +1476,11 @@
" Args:\n",
" flavors: all available ice cream flavors\n",
" Returns:\n",
" - a bowl\n",
" - a Bowl instance\n",
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -1538,7 +1538,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
},
{
Expand Down Expand Up @@ -1680,7 +1680,7 @@
" \"\"\"\n",
"\n",
" # Write your solution here\n",
" pass"
" return"
]
}
],
Expand All @@ -1700,7 +1700,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
14 changes: 14 additions & 0 deletions tutorial/tests/test_object_oriented_programming.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def __init__(self):
# Example 1: Person
#

# NOTE:
# When testing the classes returned by the solutions function in the Examples 1-4
# we often need to check if a class method is implemented and it's using the right attributes.
# One way to check both is to check if the class's method has the __closure__ attribute.
# Why?
# 1. If __method__ is not implemented, Python will fetch object.__method__ instead, which obviously is not a closure
# `hasattr(solution_result.__method__, "__closure__")` will return False.
# 2. If __method__ is implemented, but it's not using the class attributes, it will be a closure
# `solution_result.__method__.__closure__ is None` will return False.
# Reference: https://github.com/empa-scientific-it/python-tutorial/pull/249#discussion_r1836867387


def reference_oop_person(first_name: str, last_name: str):
class Person:
Expand Down Expand Up @@ -211,6 +222,9 @@ def validate_oop_compare_persons(solution_result):
assert (
type(solution_result).__name__ == "Person"
), "The class should be named 'Person'."
assert hasattr(
solution_result.__eq__, "__closure__"
), "Make sure that the class is properly implementing the __eq__() method."
# Check the class attributes
try:
attrs = list(vars(solution_result))
Expand Down

0 comments on commit 656d3d2

Please sign in to comment.