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

Matthew Horak
Matthew Horak
448 Points

Object-oriented Python "price" exercise.

I'm not to sure what the objective of the price setter is supposed to be. My setter,

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

seems to work fine if the objective is to take the price a customer sees and sets the _price to the base_price that would result in that price.

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)

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

3 Answers

Matthew Horak
Matthew Horak
448 Points

Thank you for the help, Kris. That worked to get the solution accepted. But, I'm still a little confused. The setter accepted,

@price.setter
def price(self, price):
    self._price = price

seems to give a Product object, myproduct, behavior a little counter-intuitive to me: myproduct._price returns the pre-tax cost of the item myproduct.price returns the post-tax cost of the item, but myproduct.price = new_price sets the pre-tax cost of an item to new_price.

This is shown in the the following sequence of commands, which is a little confusing to me:

myproduct = Product(0) myproduct.price = 8 myproduct.price 8.96

This is contrary to the behavior of an instance of the Circle class described in the lecture:

mycircle = Circle(0) mycircle.radius = 4 mycircle.radius 4.0

I am just wondering which behavior is more common and more desirable of a class. To me the latter seems best.

Thanks! Matt

It is more simple than you think. There is no need for calculations with the tax rate. Just set the price to the value passed in.

Sapir Lapid
Sapir Lapid
2,744 Points

Matthew, you are correct.

It seems to me that they didn't want to confuse students with calculations, but that lead to a bad design. Would be better to just remove the tax part altogether.

The current solution is a mismatch of values.