从超类返回 __str__ 时出错
error in returning __str__ from superclass
我的代码输出不准确。当它调用
super().__str__()
输出是
<__main__.Trip object at 0x00000251D30D5A30>
输出应该是
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)
您的 Trip
代码中的问题:
def __str__(self):
return f'...' + super().__str__()
是 Trip
不是 Driver
的子 class 也没有从 Driver
继承任何东西。 super
调用将 不会 调用 Driver
的 __str__
方法,但是 default/built-in object.__str__(self)
会调用所有 Python classes:
>>> class XYZ: pass
...
>>> obj1 = XYZ()
>>> print(obj1)
<__main__.XYZ object at 0x10e28b040>
只有当您的 class 是另一个 class 的子class 时,super()
才有效:
>>> 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__
我不知道你为什么期望 Trip
的 superclass 是 Driver
,因为旅行不是 driver,而是 Trip
涉及 Driver
,因此您当前使用 driver 实例化旅行的实现很有意义。
您唯一需要更改的是将 super()
替换为 self._driver
,这是您传递给 Trip
.[=38= 的 Driver
的一个实例]
# 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
或更简单地说:
# 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
因为 Python 的 str(obj)
calls that object's __str__
method.
我的代码输出不准确。当它调用
super().__str__()
输出是
<__main__.Trip object at 0x00000251D30D5A30>
输出应该是
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)
您的 Trip
代码中的问题:
def __str__(self):
return f'...' + super().__str__()
是 Trip
不是 Driver
的子 class 也没有从 Driver
继承任何东西。 super
调用将 不会 调用 Driver
的 __str__
方法,但是 default/built-in object.__str__(self)
会调用所有 Python classes:
>>> class XYZ: pass
...
>>> obj1 = XYZ()
>>> print(obj1)
<__main__.XYZ object at 0x10e28b040>
只有当您的 class 是另一个 class 的子class 时,super()
才有效:
>>> 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__
我不知道你为什么期望 Trip
的 superclass 是 Driver
,因为旅行不是 driver,而是 Trip
涉及 Driver
,因此您当前使用 driver 实例化旅行的实现很有意义。
您唯一需要更改的是将 super()
替换为 self._driver
,这是您传递给 Trip
.[=38= 的 Driver
的一个实例]
# 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
或更简单地说:
# 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
因为 Python 的 str(obj)
calls that object's __str__
method.