Convert a Class to a Dataclass
Rewrite a Product class (with name, price, and an in_stock flag defaulting to True) using @dataclass, then construct one from the given input and print it.
Approach: read a name and price, then an optional instock override ("True", "False", or a blank line meaning "use the default"), construct the dataclass accordingly, and print it — relying on @dataclass's auto-generated repr_.
Input: Three lines: name, price, and an in_stock override ("True", "False", or a blank line to use the default).
Output: One line: the dataclass instance's default repr, e.g. Product(name='Shirt', price=500.0, in_stock=True).
Shirt 500
Product(name='Shirt', price=500.0, in_stock=True)
- price parses as a float
- the third line is exactly "True", "False", or empty
Hint 1
@dataclass generates __init__ and __repr__ automatically from the type-hinted attributes.
Hint 2
A field with a default value (in_stock: bool = True) must come after all fields without defaults.
Hint 3
Printing a dataclass instance directly uses its auto-generated __repr__ — no manual formatting needed.
@dataclass turns the three type-hinted attributes into a full class with init, repr, and eq generated automatically. Since instock has a default of True, a Product can be constructed with just name and price when the third input line is blank, or with an explicit True/False otherwise. Printing the instance uses the generated repr_, which is exactly the format @dataclass produces.