Home > Software design >  error in returning __str__ from superclass
error in returning __str__ from superclass

Time:11-23

I'm getting an inaccurate output for my code. When it calls

super().__str__()

the output was

<__main__.Trip object at 0x00000251D30D5A30>

The output should be

Driver Id: 1 Name: John Contact: 82121355

from datetime import datetime

class Driver:
    _nextId = 1
    def __init__(self,name,contact):
        self._name = name
        self._contact = contact
        self._driverId = Driver._nextId
        Driver._nextId  = 1

    @property
    def driverId(self):
        return self._driverId
    @property 
    def name(self):
        return self._name
    @property
    def contact(self):
        return self._contact
    @contact.setter
    def contact(self, newContact):
        self._contact = newContact

    def __str__(self):
        return f'Driver Id: {self._driverId} Name: {self._name} Contact: {self._contact}'

class Trip:
    def __init__(self,tripDate,startPoint,destination,distance,driver):
        self._tripDate = tripDate
        self._startPoint = startPoint
        self._destination = destination
        self._distance = distance
        self._driver = driver

    @property
    def tripDate(self):
        return self._tripDate
    @property
    def destination(self):
        return self._destination
    @property
    def driver(self):
        return self._driver
    
    def __str__(self):
        return f'{self._tripDate}, From: {self._startPoint} To: {self._destination}\n Distance: {self._distance}km'   super().__str__()

if __name__ == '__main__':
    d1 = Driver('John','82121355')
    t1 = Trip(datetime(2021,5,30,17,45),'Empire State Building','Rockerfeller Centre','2.25',d1)
    print(t1)

CodePudding user response:

The problem in your Trip's code:

def __str__(self):
    return f'...'   super().__str__()

is that Trip is not a subclass of Driver nor does it inherit anything from Driver. The super call will not call Driver's __str__ method, but the default/built-in object.__str__(self) for all Python classes:

>>> class XYZ: pass
... 
>>> obj1 = XYZ()
>>> print(obj1)
<__main__.XYZ object at 0x10e28b040>

The super() only works if your class is a subclass of another:

>>> class Animal:
...   def __str__(self):
...     return 'Animal __str__'
... 

>>> class Dog(Animal):
...   def __str__(self):
...     return f'{super().__str__()}   Dog __str__'
... 

>>> d = Dog()
>>> print(d)
Animal __str__   Dog __str__

I don't know why you expect Trip's superclass to be Driver, because trips are not drivers, rather a Trip involves a Driver, so your current implementation where you instantiate a trip with a driver makes sense.

The only thing you need to change is to replace super() with self._driver which is an instance of Driver that you pass to Trip.

# super().__str__() --> self._driver.__str__()

def __str__(self):
    return f'{self._tripDate}, From: {self._startPoint} To: {self._destination}\n Distance: {self._distance}km'   self._driver.__str__()
2021-05-30 17:45:00, From: Empire State Building To: Rockerfeller Centre
 Distance: 2.25kmDriver Id: 1 Name: John Contact: 82121355

or more simply:

# super().__str__() --> str(self._driver)
# And put it inside the f-string

def __str__(self):
    return f'{self._tripDate}, From: {self._startPoint} To: {self._destination}\n Distance: {self._distance}km {str(self._driver)}'
2021-05-30 17:45:00, From: Empire State Building To: Rockerfeller Centre
 Distance: 2.25km Driver Id: 1 Name: John Contact: 82121355

since Python's str(obj) calls that object's __str__ method.

  • Related