Dataclasses, enumerations and types · 7 / 15 Exercise Easy

Point with a dataclass

Objective

Create a Python program with your first dataclass: a class that generates its own constructor and its representation.

Instructions

  • Import dataclass from dataclasses and define @dataclass class Point with two integer attributes, x and y.
  • Add the method distance_to_origin(self), which returns (x ** 2 + y ** 2) ** 0.5.
  • Read a line with two integers, create the point and show the object itself with print(point) and, on the next line, Distance: D with 2 decimals.
Input
3 4
Output
Point(x=3, y=4)
Distance: 5.00

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise