# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("7_magic_methods_horseshoe_q", "week_7", "readings", assignment_points = 22.0, assignment_tag = 'week7-readings')
# Initialize Otter
import otter
grader = otter.Notebook("7_magic_methods_horseshoe_q.ipynb")
๐ฆ Magic Methods and the โBlue Bloodโ of Horseshoe Crabs#
Horseshoe crabs have survived for hundreds of millions of years, thanks in part to their unique blue blood. This blood contains hemocyanin, which transports oxygen and has remarkable antibacterial properties, making it vital for modern medicine.
In Python, magic methods (also called dunder methods) give classes special abilities, just as hemocyanin grants horseshoe crabs extraordinary resilience. These methods allow objects to support addition, string representation, comparisons, and more.
Letโs explore how magic methods empower Python classes, just as the blue blood of horseshoe crabs has ensured their survival through the ages!
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_magic_methods_horseshoe_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_magic_methods_horseshoe_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._7_magic_methods_horseshoe_q import Question3
Question3().show()
Question 1 (Points: 13.0): ๐ฆ Free Response: Implement Magic Methods in a HorseshoeCrab
Class#
Design a Python class HorseshoeCrab
that includes:
__init__
: Initializes attributes forspecies
(str) andhemocyanin_level
(int).__str__
: Returns a string in the format"Horseshoe Crab of species <species>, hemocyanin level: <hemocyanin_level>"
.__add__
: Combines two crabsโ hemocyanin levels when using+
. Add must make sure that the species of both crabs is the same, and should return a newHorseshoeCrab
instance. if the crabs are not the same species returnValueError("Crabs must be of the same species to combine hemocyanin levels.")
.__eq__
: ReturnsTrue
if two crabs have the samespecies
andhemocyanin_level
.
Example Usage
h1 = HorseshoeCrab("Limulus polyphemus", 100)
h2 = HorseshoeCrab("Limulus polyphemus", 150)
print(h1) # Output: "Horseshoe Crab of species Limulus polyphemus, hemocyanin level: 100"
h3 = h1 + h2
print(h3) # Output: "Horseshoe Crab of species Limulus polyphemus, hemocyanin level: 250"
print(h1 == h2) # Output: False
...
# Example usage
h1 = HorseshoeCrab("Limulus polyphemus", 100)
h2 = HorseshoeCrab("Limulus polyphemus", 150)
h3 = h1 + h2
print(h3) # Expected: "Horseshoe Crab of species Limulus polyphemus, hemocyanin level: 250"
print(h1 == h2) # Expected: False
grader.check("HorseshoeCrab-Magic-Methods")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week7-readings", "7_magic_methods_horseshoe_q")