Invoice as a dataclass

Easy ⏱ 8 min 79% acceptance ★★★★☆ 4.3
Rewrite the boilerplate away: define Invoice with @dataclass holding number: str, amount: float, and paid: bool = False, then add a regular method outstanding() returning amount when unpaid else 0. Dataclasses generate __init__, __repr__ and __eq__ for you.

Examples

Example 1
Input
Invoice('INV-1', 500.0).outstanding()
Output
500.0
Explanation

paid defaulted to False so the amount is outstanding.

Constraints

  • Use @dataclass with type hints.
  • paid defaults to False.

Topics

OOPdataclasses

Companies

SalesforceAtlassianServiceNow

Hints

Hint 1

from dataclasses import dataclass.

Hint 2

Methods work exactly as in normal classes.

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