1

I still don't fully understand when and how to use properties. Here I have a class SpecialCar which is inheriting Car. The variable summer_tire should basically be equivalent to tire, except for the name. So whenever I am asking for either of those two, I want to get summer_tire.

Using @property results in an error. Deleting the @property line will print 0, but I want to get 2.

class Car():
    def __init__(self):
        self.tire = 0

class SpecialCar(Car):
    def __init__(self):
        Car.__init__(self)
        self.summer_tire = 2
        self.winter_tire = 5

    @property
    def tire(self):
        return self.summer_tire

i = SpecialCar()
print(i.tire)
1
  • Not an answer, but worth asking, if the property isn't computed why is it not a "real" property? Commented Feb 2, 2015 at 16:28

1 Answer 1

4

You declared a property that doesn't have a setter, thus self.tire = 0 in Car.__init__ fails.

You could give your new property a setter:

class SpecialCar(Car):
    def __init__(self):
        Car.__init__(self)
        self.summer_tire = 2
        self.winter_tire = 5

    @property
    def tire(self):
        return self.summer_tire

    @tire.setter
    def tire(self, new_tire):
        self.summer_tire = new_tire

or you could avoid calling Car.__init__ altogether, or make Car.tire a class attribute, set as part of the class and replaced with the property in subclasses.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.