Store-Wide Tax, Item-Specific Price

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.5
Create a class Item with a class attribute tax_rate = 0.18 shared by all items and an instance attribute price. Method gross() returns price with tax. Changing Item.tax_rate must affect every existing instance — that is the point of putting it on the class.

Examples

Example 1
Input
a = Item(100); Item.tax_rate = 0.05; a.gross()
Output
105.0
Explanation

The class-level change reaches the already-created instance.

Constraints

  • tax_rate lives on the class, not in __init__.
  • gross reads it via self (so overrides still work).

Topics

OOPclass attributes

Companies

AmazonWalmartFlipkart

Hints

Hint 1

Define tax_rate directly in the class body.

Hint 2

self.tax_rate finds the class attribute when no instance one exists.

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