Medium: Interview Core · Problem 56 of 82

A Task Dataclass

MediumDecorators and AdvancedPython

Write a dataclass Task with the fields, in this order:

  • priority (an int),
  • name (a string),
  • done, which defaults to False,
  • tags, a list which defaults to a new empty list for each task.

Tasks must be sortable and comparable using only priority and name (in that order). done and tags must not take part in comparisons or equality.

Understand the problem

Learn the three features you need.

What does @dataclass(order=True) add to the class?

  1. Nothing
  2. The comparison methods <, <=, >, >=, comparing the fields as a tuple in order
  3. Sorting of the fields alphabetically

order=True generates __lt__ and friends that compare the objects like tuples of their fields, in the order the fields are declared. That is why priority must be declared first.

What does tags: list = [] do in a dataclass?

  1. Works like any default
  2. Raises ValueError: mutable default is not allowed
  3. Makes tags read-only

A dataclass refuses a list, dict or set as a default, because all instances would share it (the mutable default trap). Use field(default_factory=list) so that each instance gets a new list.

How do you keep a field out of comparisons and equality?

  1. field(compare=False)
  2. field(hidden=True)
  3. Make it private

field(compare=False) removes the field from __eq__ and the ordering methods. field(default_factory=list, compare=False) combines both needs for tags.

Write the solution

Write the dataclass.

from dataclasses import dataclass, field

@dataclass
class Task:
    # Write your code here.
    pass
Hint 1: a question to ask yourself

Which fields take part in comparisons, and which fields have defaults?

Hint 2: the shape of the answer

@dataclass(order=True) and the four fields with type hints. done: bool = field(default=False, compare=False) and tags: list = field(default_factory=list, compare=False).

Reflect

Solution

from dataclasses import dataclass, field

@dataclass(order=True)
class Task:
    priority: int
    name: str
    done: bool = field(default=False, compare=False)
    tags: list = field(default_factory=list, compare=False)

What this tests: @dataclass, order=True, field(default_factory=...), field(compare=False) and why fields that have defaults come last.

What a dataclass writes for you: __init__, __repr__, __eq__, and with order=True the ordering methods; with frozen=True it makes the instances read-only and hashable. That removes pages of boilerplate.

Fields with defaults must come after fields without. A function cannot have a required parameter after an optional one; the generated __init__ has the same rule.

default_factory is called for each new instance. field(default_factory=list) is the dataclass version of the None default you used in the mutable-default problem.

Equality ignoring some fields: two tasks with the same priority and name are "the same task" even if one is finished. This is a design choice; say so in a docstring.

What an interviewer might ask next: Make Task immutable and usable as a dictionary key. (frozen=True, and use a tuple instead of a list for tags.)