Immutable Coordinates

Medium ⏱ 12 min 46% acceptance ★★★★☆ 4.3
Define Coord as a frozen dataclass with lat: float and lon: float — assignment after construction must raise — and write try_move(c) that attempts c.lat = 0 and returns 'immutable' when the expected FrozenInstanceError/AttributeError fires. Frozen dataclasses are also hashable, so they work in sets.

Examples

Example 1
Input
try_move(Coord(12.9, 77.6))
Output
'immutable'
Explanation

Frozen instances reject attribute assignment.

Constraints

  • @dataclass(frozen=True).
  • Catch the exception in try_move.

Topics

OOPfrozen dataclass

Companies

GoogleUberMapbox

Hints

Hint 1

dataclasses.FrozenInstanceError subclasses AttributeError.

Hint 2

Catching AttributeError covers it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.