RCPotatoSoup
RCPotatoSoup

Reputation: 19

My output is printing incorrectly and I can't seem to understand why

Here's my code. I thought I did everything right, and this is my last resort

class Vehicle(object):
    def __init__(self, make, model, year=2000):
        self.make = make
        self.model = model
        self.year = year 

    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, year):
        self.year = None
        if (year > 2000 and year < 2018):
            self._year = year

    @property
    def make(self):
        return self._make

    @make.setter
    def make(self, value):
        self._make = value

    @property
    def model(self):
        return self._model

    @model.setter
    def model(self, value):
        self._model = value

class Truck(Vehicle):
    def __init__(self, make, model, name=None):
        Vehicle.__init__(self, make, model, name)
        self.name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

class Car(Vehicle):
    def __init__(self, make, model, name=None):
        Vehicle.__init__(self, make, model, name)
        self.name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value


class DodgeRam(Truck):
    make = "Dodge"
    model = "Ram"

    def __init__(self, year):
        Truck.__init__(self, year, DodgeRam.make, DodgeRam.model)
        self.year = year

    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, year):
        self._year = year

class HondaCivic(Car):
    make = "Honda"
    model = "Civic"

    def __init__(self, year):
        Car.__init__(self, year, HondaCivic.make, HondaCivic.model)
        self.year = year

    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, year):
        self._year = year

ram = DodgeRam(2016)
print(ram)

civic1 = HondaCivic(2007)
print (civic1)

civic2 = HondaCivic(1999)
print (civic2)

Output - this is where im confused. What causes an output like this instead of a straight up error?

<main.DodgeRam object at 0x000001F04AB1A848>

<main.HondaCivic object at 0x000001F04AAA73C8>

<main.HondaCivic object at 0x000001F04AAA7DC8>

Expected Output -

2016 Dodge Ram

2007 Honda Civic

2000 Honda Civic

Upvotes: 1

Views: 22

Answers (1)

SteveJ
SteveJ

Reputation: 3313

Welcome, Mr. Soup, to stack overflow and to Python!!!

The simple answer to your question (if I understand it correctly) to get the print that you desire, is to override Python's str method. I've shown an example below. Also, I've done a few code cleanups that I hope you will find helpful.

Note that in Python, you don't assign properties (setters/getters) unless you explicitly need to interrupt them (as with year). In other languages you do (Java, C#), but not Python.

class Vehicle(object):
    def __init__(self, make: str, model: str, year: int = 2000):
        self.make = make
        self.model = model
        self.year = year

    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, year):
        if year < 1999 or year > 2019:
            raise ValueError
        self._year = year

    def __str__(self):
        return f"{self.year} {self.make} {self.model}"


class Truck(Vehicle):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)


class Car(Vehicle):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)


class DodgeRam(Truck):
    def __init__(self, year):
        super().__init__("Dodge", "Ram", year)


class HondaCivic(Car):
    def __init__(self, year):
        super().__init__("Honda", "Civic", year)


if __name__ == '__main__':

    ram = DodgeRam(2016)
    print(ram)

    civic1 = HondaCivic(2007)
    print(civic1)

    civic2 = HondaCivic(1999)
    print(civic2)

Upvotes: 1

Related Questions