0%

Dataclasses and Value Objects · practice

Keep Class-related Helpers Close

Some helpers belong closely to a class’s subject but need neither an nor the class itself.

Converting a raw into a class’s preferred form is one:

Try it

A static method receives nothing automatically

@staticmethod leaves the ’s alone. There is no self and no cls. It can be called through the class:

Duration.split_seconds(150)

Why is split_seconds a static method rather than a class method?

A module function is also honest

This kind of helper could be written outside the class:

def split_seconds(total):
    return total // 60, total % 60

That version is equally testable and often easier to reuse. Keep a static when the helper is so closely associated with the class that callers would naturally look there. Otherwise prefer the -level function.

The choice is organizational, not magical. Static methods gain no access and no special performance.

The exercise adds one class-related helper and routes correctness through it. The alternative constructors from the previous lesson are already complete so only this decision is graded.

Task

Add Question.normalize(response) as a static returning the text stripped and lowercased.

Update Question.is_correct(response) to normalize both the response and stored answer, so " DEF " matches "def".

The existing alternative constructors are complete. Do not change their construction behavior. Keep Question’s __post_init__ validation intact.