【发布时间】:2021-05-30 04:35:43
【问题描述】:
class Point4D(object):
def __init__(self,w, x, y, z):
self.w = w
self.x = x
self.y = y
self.z = z
def __str__(self):
print('{}, {}, {}, {}'.format(self.w, self.x, self.y, self.z))
my_4d_point = Point4D(1, 2, 3, 1)
print(my_4d_point)
我得到输出 1 2 3 1 ,但我不断收到错误
TypeError: __str__ returned non-string (type NoneType) in line 12. 为什么?
【问题讨论】:
-
__str__应该返回一个字符串。 -
因为你的
__str__函数返回None。