โœ๏ธ Implementing Change Methods in Classes: The Evolution of Helvetica

# 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("5_change_methods_q", "week_7", "readings", assignment_points = 26.0, assignment_tag = 'week7-readings')

# Initialize Otter
import otter
grader = otter.Notebook("5_change_methods_q.ipynb")

โœ๏ธ Implementing Change Methods in Classes: The Evolution of Helvetica#

Helvetica, one of the most iconic typefaces, has undergone several refinements since its creation in 1957 by Swiss type designer Max Miedinger. Originally named Neue Haas Grotesk, it was later adapted and rebranded as Helvetica to appeal to international audiences.

Just as Helvetica was updated and refined over time, we often need to modify objects in Python classes by implementing change methods that update attributes dynamically. Letโ€™s explore how Python change methods can help model the evolution of Helvetica in an object-oriented way!

# Run this block of code by pressing Shift + Enter to display the question
from questions._5_change_methods_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._5_change_methods_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._5_change_methods_q import Question3
Question3().show()

Question 1 (Points: 17.0): ๐Ÿ–‹๏ธ Free Response: Implement Change Methods for a Font Class#

Design a Python class Font that models the evolution of Helvetica. Your class should:

  • Have attributes for name, size, and weight (e.g., "Helvetica", 12, "Regular").

  • Include the following change methods:

    • update_name(self, new_name): Changes the font name.

    • set_size(self, new_size): Updates the font size.

    • change_weight(self, new_weight): Adjusts the font weight.

    • Return self in each method to allow method chaining.

Example Usage#

f = Font("Helvetica", 12, "Regular")
f.update_name("Helvetica Neue").set_size(14).change_weight("Bold")
print(f.name, f.size, f.weight)  # Output: Helvetica Neue 14 Bold
...

# Example usage
f = Font("Helvetica", 12, "Regular")
f.update_name("Helvetica Neue").set_size(14).change_weight("Bold")
print(f.name, f.size, f.weight)  # Expected: Helvetica Neue 14 Bold
grader.check("Helvetica-Class-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", "5_change_methods_q")