Problem overview
The task is a Product class with a name, a price in cents and a discount percent. Two rules protect the data. The price can never go below 0, and the discount always stays between 0 and 100. Other code reaches the price and the discount through properties, so the class can enforce those rules every time a value is set.
Take Product("Pen", 250). Its discount starts at 0. After p.discount = 10, final_price() returns 225, because 10% of 250 cents is 25 cents. Then p.price = -40 is refused, so reading p.price still gives 250. For Product("Lamp", 999), setting the discount to 150 stores 100, so the final price is 0; setting it to -20 afterwards stores 0.
Approach: two different rules
The two setters treat bad input differently, and the difference is the heart of the lesson.
- A negative price is ignored: the old price stays.
- A discount outside 0 to 100 is clamped to the nearest allowed value: below 0 becomes 0, above 100 becomes 100.
Python's tool for this is the property. A method marked @property runs when code reads p.price, and a method marked @price.setter runs when code writes p.price = 5. The caller still writes plain attribute syntax, with no parentheses, while the class keeps the real value in self._price. The leading underscore is only a convention: it tells other programmers not to touch the field directly. Python does not enforce it.
The name stays a plain public attribute because it has no rule to protect.
Step by step
- In
__init__, storenameinself.name. - Store
priceinself._priceand startself._discountat 0. - Write the
pricegetter: it returnsself._price. The@propertyline above it makesp.pricecall it. - Write the
pricesetter, decorated with@price.setter, taking the new price asvalue. Assign it only when it is 0 or more. When it is negative, do nothing. - Write the
discountgetter the same way. - In the
discountsetter, handle the low side first: ifvalueis below 0, store 0. - Add an
eliffor the high side: ifvalueis above 100, store 100. - Add an
elsethat storesvalue. The values 0 and 100 themselves land here and are kept. - Write
final_price: multiply the price by the percent the buyer still pays,100 - self._discount, then divide by 100 with//.
The solution in Python
class Product:
def __init__(self, name, price):
self.name = name
self._price = price
self._discount = 0
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if value >= 0:
self._price = value
@property
def discount(self):
return self._discount
@discount.setter
def discount(self, value):
if value < 0:
self._discount = 0
elif value > 100:
self._discount = 100
else:
self._discount = value
def final_price(self):
return self._price * (100 - self._discount) // 100
Why multiply first, then use //
Prices are whole cents, so the answer must be a whole number rounded down. The order matters. For a price of 999 and a discount of 15, the product is 999 * 85, which is 84915, and 84915 // 100 is 849. In ordinary division, 999 * 0.85 is 849.15, and rounding down gives the same 849, but integer arithmetic keeps everything exact and never touches floating point. Dividing first (999 // 100 * 85) would throw away the cents before the discount is applied and give 765, which is wrong.
// floors toward negative infinity. That never matters here, since the price and the factor are both 0 or more.
Using the class
p = Product("Pen", 250)
print(p.price)
p.discount = 10
print(p.final_price())
p.price = -40
print(p.price)
250
225
250
p = Product("Lamp", 999)
p.discount = 150
print(p.discount, p.final_price())
p.discount = -20
print(p.discount)
100 0
0
p = Product("Mug", 999)
p.discount = 15
print(p.final_price())
p.price = 0
print(p.final_price())
849
0
Complexity
Every getter, setter and final_price is a few comparisons and one multiplication, so each takes constant time, and a product uses constant space. The test cases only run up to a thousand operations on one product.
Why properties and not plain fields
With a plain field, p.price = -40 would store -40 and nothing could stop it. A property keeps the nice syntax and puts a method between the caller and the stored value. The same idea shows up whenever a value must obey a rule: the rule lives in one place, the setter, and every assignment goes through it, including the ones made by code you haven't written yet.
The lesson's tests only read and write through the properties, so the class is free to change how it stores the values later. Callers would not notice.
Edge cases and common mistakes
- A price of 0 is allowed. The check is
value >= 0, notvalue > 0. A free product stays 0 after any discount. - A discount of exactly 0 or 100 is kept. The branches use strict comparisons (
< 0,> 100), so both limits fall into theelseand are stored as given. - Calling a property like a method. After
@property,p.pricealready gives the number. Writingp.price()raisesTypeError: 'int' object is not callable. - Forgetting the setter decorator. A property without a setter cannot be assigned to; Python raises an
AttributeError. - Writing to
self.priceinside the price setter. That calls the setter again and recurses without end. Store intoself._price. - Clamping the price too. The price is ignored when negative, not set to 0.
