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

setters

i failing to understand how i can make it

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

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

    @price.setter
    def price(self, new_price):
        self._price = new_price/(1 + tax_rate)

1 Answer

Jimmy Sweeney
Jimmy Sweeney
5,649 Points

Hi Dennis,

Looks like you removed the @property price method. You want to keep that method and create a new setter method under the property method. The setter method will allow you to change the price of an instance later on. So, for example, if you did:

cheese = Product(2.50)
print(cheese.price)

This would tell you the price of cheese with the tax included. If you add a setter method (@price.setter), you would then be able to change the base price of cheese (_price is an attribute of the "Product" class). Like so:

cheese = Product(2.50)
print(cheese.price)
cheese.price = 3.00
print(cheese.price)

The terminal would now print out the updated total price. If you did not create a setter method, you wouldn't be able to change the _price attribute of the instance "cheese". This a little confusing in this example because what you are doing is updating the attribute "_price" even though there is also a method called "price" which returns the total cost of the cheese.

Hopefully that's not all too confusing. To sum up: You want to keep the property method "price" that was there before. You want to add a new setter method below the price method which takes an argument, "new_price", and then updates the "_price" attribute. It don't want to give away the answer but it will look like this:

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

    @price.setter
    def price(self, new_price):
        # set the attribute _price to the new price

thans @ James