Dataclasses, enumerations and types · 8 / 15 Exercise Easy

Comparing dataclasses

Objective

Create a Python program that compares two objects of a dataclass: @dataclass automatically generates __eq__, which compares attribute by attribute.

Instructions

  • Define @dataclass class Product with name: str and price: int.
  • Read two lines, each with the format name price (the name is a single word), and create a product with each one.
  • Show the two products with print, one per line, and on the third line Equal or Different depending on the result of ==.
Input
pan 2
pan 2
Output
Product(name='pan', price=2)
Product(name='pan', price=2)
Equal

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

Start exercise