假设我有一个自定义类:如何打印包括自定义类对象的列表
class A:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return self.x + ',' + self.y
我能得到预期的输出“X,Y”通过这样的:
sample = A('x', 'y')
print(sample)
但如果我把样品中的列表:
l = [sample]
print(l)
我得到这个:
[<__main__.A object at 0x7ff8ae170470>]
如何获得预期的输出?
对象是使用'__repr__'显示,而不是'__str__'。 – jonrsharpe