Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Greg Kaleka
Greg Kaleka
39,021 Points

Python Setter Method - what am I doing wrong?

I'm getting the error message "Didn't correctly set @price property".

I think what I have here makes sense. We want to set the base price (_price) attribute, which we do by dividing by 1+tax_rate. Right? What am I missing?

product.py
class Product:
    _price = 0.0
    tax_rate = 0.12

    def __init__(self, base_price):
        self._price = base_price

    @property
    def price(self):
        return self._price + (self._price * self.tax_rate)

    # this part is my code:
    @price.setter
    def price(self, price):
        self._price = price / (1 + self.tax_rate)

1 Answer

Hi Greg,

I took it very literally to just set the price and not do any calculation on it first.

Greg Kaleka
Greg Kaleka
39,021 Points

But that cannot be right.

some_product.price = 1
print(some_product.price) #prints 1.12!!

The price property is the net price after tax. The _price attribute is the base price. You can't just set the base price equal to the price after tax.

Well, we don't really know the details here of how this gets used but this is how I'm reasoning it out.

You initialize the product with the base price. Whenever you get the price for the product it should be the tax adjusted price.

The base price of the product might change at some point in the future and the setter is how you would reset that base price.

I think you're probably looking at it as, the price passed in is including the tax and you need to work out the base price.

But I suspect we're just literally passing in the base price when we use the setter.